saveNEVSubSpikes.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. function saveNEVSubSpikes(channelsToRead, fileFullPath, addedSuffix)
  2. % saveNEVSubSpikes
  3. %
  4. % Opens saves a new NEV file that only contains chanenls in channelsToRead.
  5. %
  6. % Use saveNEVSubSpikes(channelsToRead)
  7. %
  8. % channelsToRead: The channel data to be saved into a new NEV.
  9. % DEFAULT: This input is required.
  10. %
  11. % fileFullPath: The full path to the NEV file that is going to be
  12. % used for splitting.
  13. % DEFAULT: The user will be prompted to choose a suffix.
  14. %
  15. % addedSuffix: The suffix added to the end of splitted file.
  16. % DEFAULT: 'ss' is the added suffix.
  17. %
  18. % Example 1:
  19. % channelsToRead(4, 'c:\datafolder\datafile.nev', 'tet');
  20. %
  21. % In the example above, the file c:\datafolder\datafile.nev will be used.
  22. % The selected NEV file will be saved into a new NEV file that only
  23. % contains data from channel 4. The new file will have the added suffix
  24. % 'tet', so the new filename will be c:\datafolder\datafile-ssXXX.nev.
  25. %
  26. % Example 2:
  27. % channelsToRead([5,8,12];
  28. %
  29. % In the example above, the user will be prompted to select a NEV file.
  30. % The selected NEV file will be saved into a new NEV file that only
  31. % contains data from channels 5, 8, and 12.
  32. %
  33. % Kian Torab
  34. % ktorab@blackrockmicro.com
  35. % Blackrock Microsystems
  36. % Version 1.2.2.0
  37. %
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. % Version History
  40. %
  41. % 1.0.0.0: Initial release.
  42. %
  43. % 1.1.0.0:
  44. % - Bug fix with saving file names.
  45. % - Added ability to pass the file name to the function as an argument.
  46. % - Added ability to define the suffix to the split files (addedExtension).
  47. %
  48. % 1.2.0.0:
  49. % - Fixed a bug related to the # of input arguments and compatibility
  50. % with other functions.
  51. %
  52. % 1.2.1.0:
  53. % - Updated help.
  54. %
  55. % 1.2.2.0:
  56. % - Fixed a bug where the data was not being saved correctly on Windows
  57. % machines.
  58. % - Fixed a bug where tetrodes higher than 10 were overwriting tetrodes 1
  59. % through 10 over and over again.
  60. %
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. %% Validating the input argument
  63. if ~exist('channelsToRead', 'var')
  64. disp('channelsToRead is a required input argument.');
  65. return;
  66. else
  67. channelsToRead = [channelsToRead 65535:-1:65520];
  68. end
  69. %% Setting the default for addedExtension
  70. if ~exist('addedSuffix', 'var')
  71. addedSuffix = 'ss';
  72. end
  73. %% Opening the file and reading header
  74. if ~exist('fileFullPath', 'var')
  75. [dataFilename dataFolder] = getFile('*.nev');
  76. fileFullPath = [dataFolder dataFilename];
  77. end
  78. FID = fopen(fileFullPath, 'r', 'ieee-le');
  79. %% Calculating the header bytes
  80. BasicHeader = fread(FID, 20, '*uint8');
  81. headerBytes = double(typecast(BasicHeader(13:16), 'uint32'));
  82. dataPacketByteLength = double(typecast(BasicHeader(17:20), 'uint32'));
  83. %% Calculating the data file length and eeking to the beginning of the file
  84. fseek(FID, 0, 'eof');
  85. endOfDataByte = ftell(FID);
  86. dataByteLength = endOfDataByte - headerBytes;
  87. numberOfPackets = double(dataByteLength) / double(dataPacketByteLength);
  88. %% Reading the header binaries and saving it for future
  89. fseek(FID, 0, 'bof');
  90. headerBinaries = fread(FID, headerBytes, '*uint8');
  91. %% Reading the data binaries
  92. dataBinaries = fread(FID, [dataPacketByteLength numberOfPackets], '*uint8', 0);
  93. %% Finding what PacketIDs have the desired channels
  94. for IDX = 1:size(dataBinaries,2)
  95. PacketIDs(IDX) = typecast(dataBinaries(5:6, IDX), 'uint16');
  96. end
  97. packetIDIndices = [];
  98. for IDX = 1:length(channelsToRead)
  99. packetIDsFound = find(PacketIDs == channelsToRead(IDX));
  100. packetIDIndices(length(packetIDIndices)+1:length(packetIDIndices)+length(packetIDsFound)) = packetIDsFound;
  101. end
  102. packetIDIndices = sort(packetIDIndices);
  103. %% Truncating the data to only contain the desired channels
  104. newDataBinaries = dataBinaries(:, packetIDIndices);
  105. %% Determining the file name
  106. currentFileNames = dir([fileFullPath(1:end-4) '-' addedSuffix '*.nev']);
  107. if ~isempty(currentFileNames)
  108. fileIDX = str2num(currentFileNames(end).name(end-6:end-4))+1;
  109. else
  110. fileIDX = 1;
  111. end
  112. %% Saving the new NEV containig the desired channels
  113. FIDw = fopen([fileFullPath(1:end-4) '-' addedSuffix sprintf('%03d', fileIDX) fileFullPath(end-3:end)], 'w+', 'n');
  114. fwrite(FIDw, headerBinaries, 'uint8');
  115. fwrite(FIDw, newDataBinaries, 'uint8');
  116. fclose(FID);
  117. fclose(FIDw);