Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

splitNSx.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. % 1.2.0.0:
  35. % - Updated to add FileSpec 3.0 compatibility. - @David Kluger
  36. %
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. %
  39. % Validating input parameter
  40. if ~exist('splitCount', 'var')
  41. splitCount = 2;
  42. end
  43. % Getting the file name
  44. if ~ismac
  45. [fname, path] = getFile('*.ns*', 'Choose an NSx file...');
  46. else
  47. [fname, path] = getFile('*.*', 'Choose an NSx file...');
  48. end
  49. if fname == 0
  50. disp('No file was selected.');
  51. if nargout
  52. clear variables;
  53. end
  54. return;
  55. end
  56. fext = fname(end-3:end);
  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. disp(['Splitting the NSx file in ' num2str(splitCount) ' pieces...']);
  65. if strcmpi(NSx.MetaTags.FileTypeID, 'NEURALSG')
  66. disp('File type 2.1 is not yet implemented.');
  67. %NOT IMPLEMENTED YET
  68. % fseek(FID, 0, 'bof');
  69. % header = fread(FID, 314,'*uint8');
  70. % positionEOH = ftell(FID);
  71. % fseek(FID, 0, 'eof');
  72. % positionEOD = ftell(FID);
  73. % dataLength = positionEOD - positionEOH;
  74. % fseek(FID, 28, 'bof');
  75. % channelCount = fread(FID, 1 , 'uint32=>double');
  76. elseif strcmpi(NSx.MetaTags.FileTypeID, 'NEURALCD') || strcmpi(NSx.MetaTags.FileTypeID, 'BRSMPGRP')
  77. % Calculating different points in the file
  78. fseek(FID, 0, 'bof');
  79. basicHeader = fread(FID, 314, '*uint8');
  80. positionEOE = typecast(basicHeader(11:14), 'uint32');
  81. fseek(FID, 0, 'eof');
  82. positionEOD = ftell(FID);
  83. % Calculating channelCount, data Length
  84. channelCount = typecast(basicHeader(311:314), 'uint32');
  85. dataLength = positionEOD - positionEOE - 9;
  86. % Reading the number of packets
  87. fseek(FID, 28, 'bof');
  88. numOfPackets = (dataLength)/(2*channelCount);
  89. % Calculating the number of bytes in each segment
  90. segmentBytes = floor(numOfPackets/splitCount)*(2*channelCount);
  91. % Reading the headers and the data header
  92. fseek(FID, 0, 'bof');
  93. fileHeader = fread(FID, positionEOE, 'char');
  94. dataHeader = fread(FID, 9, 'char');
  95. fseek(FID, positionEOE+9, 'bof');
  96. for idx = 1:splitCount
  97. % Opening a file for saving
  98. FIDw = fopen([path fname(1:end-4) '-s' sprintf('%03d', idx) fname(end-3:end)], 'w+', 'ieee-le');
  99. fprintf('\nReading segment %d... ', idx);
  100. % Reading the segment
  101. dataSegment = fread(FID, segmentBytes, 'char');
  102. fprintf('Writing segment %d... ', idx);
  103. % Writing the segmented data into file
  104. fwrite(FIDw, fileHeader, 'char');
  105. % Set the timestamp of the segments 2+ to 0 so there's no
  106. % introduced shift by openNSx.
  107. if idx > 1
  108. dataHeader(2:5) = 0;
  109. end
  110. fwrite(FIDw, dataHeader, 'char');
  111. fwrite(FIDw, dataSegment, 'char');
  112. % Clearing variables and closing file
  113. clear dataSegment;
  114. fclose(FIDw);
  115. end
  116. fprintf('\n');
  117. else
  118. % Display error if non-compatible file is trying to open.
  119. disp('This version of splitNSx can only split File Specs 2.2 and 2.3');
  120. disp(['The selected file spec is ' NSx.MetaTags.FileSpec '.']);
  121. fclose(FID);
  122. clear variables;
  123. return;
  124. end