separatePausedNSx_old.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function separatePausedNSx(varargin)
  2. % separatePausedNsx Saves paused data segments from a single NSx file
  3. % as individual NSx files
  4. %
  5. % separatePausedNSx(FILENAME) where FILENAME has N paused segments
  6. % will create N individual files with the same extension named
  7. % FILENAME-1, FILENAME-2, ..., FILENAME-N.
  8. %
  9. % separatePausedFiles without any input arguments opens a UIgetfile to
  10. % select the NSx file to separate
  11. %
  12. % Brett Dowden
  13. % bdowden@blackrockmicro.com
  14. % Nick Halper
  15. % nhalper@blackrockmicro.com
  16. % Blackrock Microsystems
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. % Version History
  19. %
  20. % 1.0.0.0: Initial release
  21. %
  22. % 1.1.0.0:
  23. % - Fixed a broken function that dependent on a non-existant saveNEV
  24. % script.
  25. %
  26. % 1.2.0.0:
  27. % - Corrected NSx_out.MetaTags.Filename =
  28. % [NSx.MetaTags.Filename(1:end) '-p' sprintf('%03d', i)
  29. % NSx.MetaTags.FileExt] which was incorrectly removing the last 4
  30. % characters of the filename in an attempt to remove the extension.
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. % Since openNSx checks input parameters for file validity and for no input
  33. % argument case, just pass varargin to openNSx
  34. NSx = openNSx('read',varargin{:});
  35. % Check to see if there were pauses in the file. If so, save the sections
  36. % as individual files, if not just report that to the user and end function
  37. if length(NSx.MetaTags.Timestamp) == 1
  38. disp('No pauses in data file. No action taken');
  39. else
  40. for i = 1 : length(NSx.MetaTags.Timestamp)
  41. % create a copy of NSx, except with only one data segment and the
  42. % corresponding Timestamp, DataPoints, and NumofPacket fields
  43. NSx_out.MetaTags = NSx.MetaTags;
  44. NSx_out.MetaTags.Timestamp = NSx.MetaTags.Timestamp(i);
  45. NSx_out.MetaTags.DataPoints = NSx.MetaTags.DataPoints(i);
  46. NSx_out.MetaTags.NumofPackets = NSx.MetaTags.DataPoints(i);
  47. NSx_out.ElectrodesInfo = NSx.ElectrodesInfo;
  48. NSx_out.Data = NSx.Data{i};
  49. NSx_out.RawData.Headers = NSx.RawData.Headers;
  50. %Data headers are only 9 bytes, so only check for the correct,
  51. %respective 9 bytes
  52. NSx_out.RawData.DataHeader = NSx.RawData.DataHeader(1+(9*(i-1)):9+(9*(i-1)));
  53. % Create enumerated file name
  54. NSx_out.MetaTags.Filename = [NSx.MetaTags.Filename(1:end) '-p' sprintf('%03d', i) NSx.MetaTags.FileExt];
  55. disp('Opening the original file...');
  56. % Check for filename existence
  57. newFilename = fullfile(NSx.MetaTags.FilePath, NSx_out.MetaTags.Filename);
  58. if exist(newFilename, 'file') == 2
  59. overwriteFlag = input('The file already exists. Overwrite? ', 's');
  60. if ~strcmpi(overwriteFlag, 'y')
  61. return;
  62. end
  63. end
  64. % Create new file
  65. FIDw = fopen(newFilename, 'w+', 'ieee-le');
  66. % Writing the header information
  67. fwrite(FIDw, NSx_out.RawData.Headers, 'uint8');
  68. fwrite(FIDw, NSx_out.RawData.DataHeader, 'uint8');
  69. % Writing data into file
  70. disp('Writing into the new file...');
  71. fwrite(FIDw, NSx_out.Data, 'int16');
  72. fclose(FIDw);
  73. end
  74. clear all;
  75. end