splitNSxPauses.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function splitNSxPauses(fileName)
  2. % splitNSxPauses
  3. %
  4. % Opens and splits an NSx file in smaller pieces, timewise.
  5. %
  6. % Use splitNSx(fileName)
  7. %
  8. % All input arguments are optional. Input arguments can be in any order.
  9. %
  10. % fileName: File name of the file that needs to be split.
  11. % DEFAULT: The user will be prompted to select a file.
  12. %
  13. % Example 1:
  14. % splitNSx('C:\Datafolder\mydata.ns5');
  15. %
  16. % In the example above, the file C:\Datafolder\mydata.ns5 will be opened.
  17. % The loaded file will be split in samller files representing its paused
  18. % sub-segments.
  19. %
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. % Kian Torab
  22. % support@blackrockmicro.com
  23. % Blackrock Microsystems
  24. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25. % Version History
  26. %
  27. % 1.0.0.0: August 31, 2016
  28. % - Initial release.
  29. % - Successor to separateNSxPaused running much more memory efficient.
  30. %
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. %% Getting the file name
  33. if ~exist('fileName', 'var')
  34. fileName = '';
  35. end
  36. if ~(exist(fileName, 'file') == 2)
  37. if ~ismac
  38. [fname, path] = getFile('*.ns*', 'Choose an NSx file...');
  39. else
  40. [fname, path] = getFile('*.*', 'Choose an NSx file...');
  41. end
  42. if fname == 0
  43. disp('No file was selected.');
  44. if nargout
  45. clear variables;
  46. end
  47. return;
  48. end
  49. fext = fname(end-3:end);
  50. else
  51. [path, fname, fext] = fileparts(fileName);
  52. if ismac path = [path '/']; else path = [path '\']; end
  53. fname = [fname fext];
  54. end
  55. %% Getting header information
  56. NSx = openNSx('noread', [path fname ]);
  57. % Loading the file
  58. %% Reading Basic Header from file into NSx structure.
  59. FID = fopen([path fname], 'r', 'ieee-le');
  60. NSx.MetaTags.Filename = fname;
  61. NSx.MetaTags.FilePath = path(1:end-1);
  62. NSx.MetaTags.FileExt = fext;
  63. NSx.MetaTags.FileTypeID = fread(FID, [1,8] , '*char');
  64. if strcmpi(NSx.MetaTags.FileTypeID, 'NEURALSG')
  65. disp('File type 2.1 is not yet implemented.');
  66. %NOT IMPLEMENTED YET
  67. % fseek(FID, 0, 'bof');
  68. % header = fread(FID, 314,'*uint8');
  69. % positionEOH = ftell(FID);
  70. % fseek(FID, 0, 'eof');
  71. % positionEOD = ftell(FID);
  72. % dataLength = positionEOD - positionEOH;
  73. % fseek(FID, 28, 'bof');
  74. % channelCount = fread(FID, 1 , 'uint32=>double');
  75. elseif strcmpi(NSx.MetaTags.FileTypeID, 'NEURALCD')
  76. % Calculating different points in the file
  77. fseek(FID, 0, 'bof');
  78. basicHeader = fread(FID, 314, '*uint8');
  79. positionEOE = typecast(basicHeader(11:14), 'uint32');
  80. fseek(FID, 0, 'eof');
  81. positionEOD = ftell(FID);
  82. % Calculating channelCount, data Length
  83. channelCount = typecast(basicHeader(311:314), 'uint32');
  84. dataLength = positionEOD - positionEOE - 9;
  85. % Reading the number of packets
  86. fseek(FID, 28, 'bof');
  87. numOfPackets = (dataLength)/(2*channelCount);
  88. % Calculating the number of splits
  89. splitCount = length(NSx.MetaTags.Timestamp);
  90. % Calculating the number of bytes in each segment
  91. segmentBytes = NSx.MetaTags.DataPoints * 2 * double(channelCount);
  92. % Reading the headers and the data header
  93. fseek(FID, 0, 'bof');
  94. fileHeader = fread(FID, positionEOE, 'char');
  95. dataHeader = fread(FID, 9, 'char');
  96. fseek(FID, positionEOE, 'bof');
  97. disp(['Splitting the NSx file in ' num2str(splitCount) ' pieces...']);
  98. for idx = 1:splitCount
  99. % Opening a file for saving
  100. FIDw = fopen([path fname(1:end-4) '-s' sprintf('%03d', idx) fname(end-3:end)], 'w+', 'ieee-le');
  101. fprintf('\nReading segment %d... ', idx);
  102. % Reading the segment
  103. fseek(FID, 9, 'cof'); % Skipping the data header
  104. dataSegment = fread(FID, segmentBytes(idx), 'char');
  105. fprintf('Writing segment %d... ', idx);
  106. % Writing the segmented data into file
  107. fwrite(FIDw, fileHeader, 'char');
  108. % Set the timestamp of the segments 2+ to 0 so there's no
  109. % introduced shift by openNSx.
  110. if idx > 1
  111. dataHeader(2:5) = 0;
  112. end
  113. fwrite(FIDw, dataHeader, 'char');
  114. fwrite(FIDw, dataSegment, 'char');
  115. % Clearing variables and closing file
  116. clear dataSegment;
  117. fclose(FIDw);
  118. end
  119. fprintf('\n');
  120. else
  121. % Display error if non-compatible file is trying to open.
  122. disp('This version of splitNSx can only split File Specs 2.2 and 2.3');
  123. disp(['The selected file spec is ' NSx.MetaTags.FileSpec '.']);
  124. fclose(FID);
  125. clear variables;
  126. return;
  127. end