# nex_ts

## PURPOSE

nex_ts(filename, varname): Read timestamps from a .nex file

## SYNOPSIS

function [n, ts] = nex_ts(filename, varname)

## DESCRIPTION

     nex_ts(filename, varname): Read timestamps from a .nex file

     [n, ts] = nex_ts(filename, varname)

     INPUT:
       filename - if empty string, will use File Open dialog
       varname - variable name
     OUTPUT:
       n - number of timestamps
       ts - array of timestamps (in seconds)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- test_nex

## SOURCE CODE

    0001 function [n, ts] = nex_ts(filename, varname)
    0002 % nex_ts(filename, varname): Read timestamps from a .nex file
    0003 %
    0004 % [n, ts] = nex_ts(filename, varname)
    0005 %
    0006 % INPUT:
    0007 %   filename - if empty string, will use File Open dialog
    0008 %   varname - variable name
    0009 % OUTPUT:
    0010 %   n - number of timestamps
    0011 %   ts - array of timestamps (in seconds)
    0012
    0013 n = 0;
    0014 ts = 0;
    0015
    0016 if(nargin ~= 2)
    0017    disp('2 input arguments are required')
    0018    return
    0019 end
    0020
    0021 if(ischar(filename) == 0)
    0022    disp('input arguments should be character arrays')
    0023    return
    0024 end
    0025
    0026 if(ischar(varname) == 0)
    0027    disp('input arguments should be character arrays')
    0028    return
    0029 end
    0030
    0031 if(isempty(filename))
    0032    [fname, pathname] = uigetfile('*.nex', 'Select a Nex file');
    0033     filename = strcat(pathname, fname);
    0034 end
    0035
    0036 fid = fopen(filename, 'r');
    0037 if(fid == -1)
    0038     disp('cannot open file');
    0039    return
    0040 end
    0041
    0042 disp(strcat('file = ', filename));
    0043 magic = fread(fid, 1, 'int32');
    0044 version = fread(fid, 1, 'int32');
    0045 comment = fread(fid, 256, 'char');
    0046 freq = fread(fid, 1, 'double');
    0047 tbeg = fread(fid, 1, 'int32');
    0048 tend = fread(fid, 1, 'int32');
    0049 nvar = fread(fid, 1, 'int32');
    0050 fseek(fid, 260, 'cof');
    0051 name = zeros(1, 64);
    0052 found = 0;
    0053 for i=1:nvar
    0054     type = fread(fid, 1, 'int32');
    0055     var_version = fread(fid, 1, 'int32');
    0056     name = fread(fid, [1 64], 'char');
    0057     offset = fread(fid, 1, 'int32');
    0058     n = fread(fid, 1, 'int32');
    0059     name = char(name);
    0060     name = deblank(name);
    0061     k = strcmp(name, deblank(varname));
    0062     if(k == 1)
    0063         found = 1;
    0064         fseek(fid, offset, 'bof');
    0065         ts = fread(fid, [1 n], 'int32');
    0066         break
    0067     end
    0068     dummy = fread(fid, 128, 'char');
    0069 end
    0070
    0071 fclose(fid);
    0072
    0073 if found == 0
    0074     disp('did not find variable in the file');
    0075 else
    0076     ts = ts/freq;
    0077     disp(strcat('number of timestamps = ', num2str(n)));
    0078 end