splitNSx.m 4.1 KB

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