openNSxHL.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. function OUTPUT = openNSxHL(fname)
  2. % openNSxHL
  3. %
  4. % Opens and reads an NSx file without the header information and returns
  5. % the binary data. This can be used for specific applications that require
  6. % this type of data, e.g. Klusters. Works with File Spec 2.1, 2.2 and 2.3.
  7. % It does not support pauses at this time.
  8. %
  9. %
  10. % Use OUTPUT = openNSx(fname)
  11. %
  12. % All input arguments are optional.
  13. %
  14. % fname: Name of the file to be opened. If the fname is omitted
  15. % the user will be prompted to select a file.
  16. % DEFAULT: Will open Open File UI.
  17. %
  18. % OUTPUT: Contains the binary data.
  19. %
  20. % Example 1:
  21. % openNSxHL('c:\data\sample.ns5');
  22. %
  23. % In the example above, the file c:\data\sample.ns5 will be opened and
  24. % the data will be read and output through variable OUTPUT.
  25. %
  26. % Example 2:
  27. % openNSxHL;
  28. %
  29. % In the example above, the file user will be prompted for the file. The
  30. % selected file will be opened and the data will be read and output
  31. % through variable OUTPUT.
  32. %
  33. % Kian Torab
  34. % kian@blackrockmicro.com
  35. % Blackrock Microsystems
  36. % Version 1.0.0.0
  37. %
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. % Version History
  40. %
  41. % 1.0.0.0:
  42. % - Initial release.
  43. %
  44. % 1.1.0.0: June 16, 2017
  45. % - Pads the data with zeros when the beginning timestamp is not 0.
  46. %
  47. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  48. %% Opening the file
  49. % Popup the Open File UI. Also, process the file name, path, and extension
  50. % for later use, and validate the entry.
  51. if ~exist('fname', 'var')
  52. if ~ismac
  53. [fname, path] = getFile('*.ns*', 'Choose an NSx file...');
  54. else
  55. [fname, path] = getFile('*.*', 'Choose an NSx file...');
  56. end
  57. if fname == 0
  58. disp('No file was selected.');
  59. if nargout
  60. clear variables;
  61. end
  62. return;
  63. end
  64. else
  65. if isempty(fileparts(fname))
  66. fname = which(fname);
  67. end
  68. [path,fname, fext] = fileparts(fname);
  69. fname = [fname fext];
  70. path = [path '/'];
  71. end
  72. if fname==0
  73. return;
  74. end
  75. %% Reading the file data
  76. % Opening the file
  77. FID = fopen([path fname], 'r', 'ieee-le');
  78. FileTypeID = fread(FID, [1,8] , '*char');
  79. % Finding the begining of the data point in the file
  80. if strcmpi(FileTypeID, 'NEURALSG')
  81. fseek(FID, 20, 'cof');
  82. ChannelCount = double(fread(FID, 1, 'uint32=>double'));
  83. fread(FID, [ChannelCount 1], '*uint32');
  84. HeaderBytes = ftell(FID);
  85. elseif strcmpi(FileTypeID, 'NEURALCD')
  86. dataHeaderBytes = 9;
  87. BasicHeader = fread(FID, 306, '*uint8');
  88. HeaderBytes = double(typecast(BasicHeader(3:6), 'uint32'));
  89. else
  90. disp('This version of NSxToXXX can only read File Specs 2.1, 2.2 and 2.3');
  91. disp(['The selected file spec is ' NSx.MetaTags.FileSpec '.']);
  92. fclose(FID);
  93. clear variables;
  94. return;
  95. end
  96. % Finding the number of channels
  97. fseek(FID, 310, 'bof');
  98. numofChannels = fread(FID, 1, '*uint32');
  99. % Skipping to the point where the data is saved, skipping the header info
  100. fseek(FID, HeaderBytes, 'bof');
  101. % Finding the beginning timestamp so it can be padded with zeros in case
  102. % it's not 0 already.
  103. fseek(FID, 1, 'cof'); % Skipping to timestamp
  104. begTimestamp = fread(FID, 1, '*uint32');
  105. paddedZeros = zeros(numofChannels * begTimestamp * 2, 1);
  106. fseek(FID, 4, 'cof'); % Skipping to data
  107. % Reading the header-less data
  108. disp(['Reading the data from file ' path fname '...']);
  109. OUTPUT = fread(FID, inf, '*int16');
  110. OUTPUT = [paddedZeros; OUTPUT];
  111. fclose(FID);