plx_event_ts.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function [n, ts, sv] = plx_event_ts(filename, ch)
  2. % plx_event_ts(filename, channel) Read event timestamps from a .plx file
  3. %
  4. % [n, ts, sv] = plx_event_ts(filename, channel)
  5. %
  6. % INPUT:
  7. % filename - if empty string, will use File Open dialog
  8. % channel - 1-based external channel number
  9. % strobed channel has channel number 257
  10. % OUTPUT:
  11. % n - number of timestamps
  12. % ts - array of timestamps
  13. % sv - array of strobed event values (filled only if channel is 257)
  14. if(nargin ~= 2)
  15. disp('2 input arguments are required')
  16. return
  17. end
  18. n = 0;
  19. ts = 0;
  20. sv = 0;
  21. if(isempty(filename))
  22. [fname, pathname] = uigetfile('*.plx', 'Select a plx file');
  23. filename = strcat(pathname, fname);
  24. end
  25. fid = fopen(filename, 'r');
  26. if(fid == -1)
  27. disp('cannot open file');
  28. return
  29. end
  30. disp(strcat('file = ', filename));
  31. % read file header
  32. header = fread(fid, 64, 'int32');
  33. freq = header(35); % frequency
  34. ndsp = header(36); % number of dsp channels
  35. nevents = header(37); % number of external events
  36. nslow = header(38); % number of slow channels
  37. npw = header(39); % number of points in wave
  38. npr = header(40); % number of points before threshold
  39. tscounts = fread(fid, [5, 130], 'int32');
  40. wfcounts = fread(fid, [5, 130], 'int32');
  41. evcounts = fread(fid, [1, 512], 'int32');
  42. % skip variable headers
  43. fseek(fid, 1020*ndsp + 296*nevents + 296*nslow, 'cof');
  44. % read the data
  45. record = 0;
  46. while feof(fid) == 0
  47. type = fread(fid, 1, 'int16');
  48. upperbyte = fread(fid, 1, 'int16');
  49. timestamp = fread(fid, 1, 'int32');
  50. channel = fread(fid, 1, 'int16');
  51. unit = fread(fid, 1, 'int16');
  52. nwf = fread(fid, 1, 'int16');
  53. nwords = fread(fid, 1, 'int16');
  54. toread = nwords;
  55. if toread > 0
  56. wf = fread(fid, toread, 'int16');
  57. end
  58. if type == 4
  59. if channel == ch
  60. n = n + 1;
  61. ts(n) = timestamp;
  62. sv(n) = unit;
  63. end
  64. end
  65. record = record + 1;
  66. if feof(fid) == 1
  67. break
  68. end
  69. end
  70. disp(strcat('number of timestamps = ', num2str(n)));
  71. fclose(fid);