# plx_ts

## PURPOSE

plx_ts(filename, channel, unit): Read spike timestamps from a .plx file

## SYNOPSIS

function [n, ts] = plx_ts(filename, ch, u)

## DESCRIPTION

     plx_ts(filename, channel, unit): Read spike timestamps from a .plx file

     [n, ts] = plx_ts(filename, channel, unit)

     INPUT:
       filename - if empty string, will use File Open dialog
       channel - 1-based channel number
       unit  - unit number (0- invalid, 1-4 valid)
     OUTPUT:
       n - number of timestamps
       ts - array of timestamps (in seconds)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

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