# plx_event_ts

## PURPOSE

plx_event_ts(filename, channel) Read event timestamps from a .plx file

## SYNOPSIS

function [n, ts, sv] = plx_event_ts(filename, ch)

## DESCRIPTION

     plx_event_ts(filename, channel) Read event timestamps from a .plx file

     [n, ts, sv] = plx_event_ts(filename, channel)

     INPUT:
       filename - if empty string, will use File Open dialog
       channel - 1-based external channel number
                 strobed channel has channel number 257
     OUTPUT:
       n - number of timestamps
       ts - array of timestamps
       sv - array of strobed event values (filled only if channel is 257)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [n, ts, sv] = plx_event_ts(filename, ch)
    0002 % plx_event_ts(filename, channel) Read event timestamps from a .plx file
    0003 %
    0004 % [n, ts, sv] = plx_event_ts(filename, channel)
    0005 %
    0006 % INPUT:
    0007 %   filename - if empty string, will use File Open dialog
    0008 %   channel - 1-based external channel number
    0009 %             strobed channel has channel number 257
    0010 % OUTPUT:
    0011 %   n - number of timestamps
    0012 %   ts - array of timestamps
    0013 %   sv - array of strobed event values (filled only if channel is 257)
    0014
    0015 if(nargin ~= 2)
    0016    disp('2 input arguments are required')
    0017    return
    0018 end
    0019
    0020 n = 0;
    0021 ts = 0;
    0022 sv = 0;
    0023
    0024 if(isempty(filename))
    0025    [fname, pathname] = uigetfile('*.plx', 'Select a plx file');
    0026     filename = strcat(pathname, fname);
    0027 end
    0028
    0029 fid = fopen(filename, 'r');
    0030 if(fid == -1)
    0031     disp('cannot open file');
    0032    return
    0033 end
    0034
    0035 disp(strcat('file = ', filename));
    0036
    0037 % read file header
    0038 header = fread(fid, 64, 'int32');
    0039 freq = header(35);  % frequency
    0040 ndsp = header(36);  % number of dsp channels
    0041 nevents = header(37); % number of external events
    0042 nslow = header(38);  % number of slow channels
    0043 npw = header(39);  % number of points in wave
    0044 npr = header(40);  % number of points before threshold
    0045 tscounts = fread(fid, [5, 130], 'int32');
    0046 wfcounts = fread(fid, [5, 130], 'int32');
    0047 evcounts = fread(fid, [1, 512], 'int32');
    0048
    0049 % skip variable headers
    0050 fseek(fid, 1020*ndsp + 296*nevents + 296*nslow, 'cof');
    0051
    0052 % read the data
    0053 record = 0;
    0054 while feof(fid) == 0
    0055     type = fread(fid, 1, 'int16');
    0056     upperbyte = fread(fid, 1, 'int16');
    0057     timestamp = fread(fid, 1, 'int32');
    0058     channel = fread(fid, 1, 'int16');
    0059     unit = fread(fid, 1, 'int16');
    0060     nwf = fread(fid, 1, 'int16');
    0061     nwords = fread(fid, 1, 'int16');
    0062     toread = nwords;
    0063     if toread > 0
    0064       wf = fread(fid, toread, 'int16');
    0065     end
    0066        if type == 4
    0067          if channel == ch
    0068                  n = n + 1;
    0069                  ts(n) = timestamp;
    0070                 sv(n) = unit;
    0071            end
    0072        end
    0073
    0074    record = record + 1;
    0075    if feof(fid) == 1
    0076       break
    0077    end
    0078
    0079 end
    0080 disp(strcat('number of timestamps = ', num2str(n)));
    0081
    0082 fclose(fid);