# nex_int

## PURPOSE

nex_int(filename, varname): Read interval variable from a .nex file

## SYNOPSIS

function [n, ts_left, ts_right] = nex_int(filename, varname)

## DESCRIPTION

     nex_int(filename, varname): Read interval variable from a .nex file

     [n, ts_left, ts_right] = nex_int(filename, varname)

     INPUT:
       filename - if empty string, will use File Open dialog
       varname - variable name
     OUTPUT:
       n - number of intervals
       ts_left - array of left ends of the intervals (in seconds)
       ts_right - array of right ends of the intervals (in seconds)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [n, ts_left, ts_right] = nex_int(filename, varname)
    0002 % nex_int(filename, varname): Read interval variable from a .nex file
    0003 %
    0004 % [n, ts_left, ts_right] = nex_int(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 intervals
    0011 %   ts_left - array of left ends of the intervals (in seconds)
    0012 %   ts_right - array of right ends of the intervals (in seconds)
    0013
    0014 n = 0;
    0015 ts_left = 0;
    0016 ts_right = 0;
    0017
    0018 if(nargin ~= 2)
    0019    disp('2 input arguments are required')
    0020    return
    0021 end
    0022
    0023 if(ischar(filename) == 0)
    0024    disp('input arguments should be character arrays')
    0025    return
    0026 end
    0027
    0028 if(ischar(varname) == 0)
    0029    disp('input arguments should be character arrays')
    0030    return
    0031 end
    0032
    0033 if(isempty(filename))
    0034    [fname, pathname] = uigetfile('*.nex', 'Select a Nex file');
    0035     filename = strcat(pathname, fname);
    0036 end
    0037
    0038 fid = fopen(filename, 'r');
    0039 if(fid == -1)
    0040     disp('cannot open file');
    0041    return
    0042 end
    0043
    0044 disp(strcat('file = ', filename));
    0045 magic = fread(fid, 1, 'int32');
    0046 version = fread(fid, 1, 'int32');
    0047 comment = fread(fid, 256, 'char');
    0048 freq = fread(fid, 1, 'double');
    0049 tbeg = fread(fid, 1, 'int32');
    0050 tend = fread(fid, 1, 'int32');
    0051 nvar = fread(fid, 1, 'int32');
    0052 fseek(fid, 260, 'cof');
    0053 name = zeros(1, 64);
    0054 found = 0;
    0055 for i=1:nvar
    0056     type = fread(fid, 1, 'int32');
    0057     var_version = fread(fid, 1, 'int32');
    0058     name = fread(fid, [1 64], 'char');
    0059     offset = fread(fid, 1, 'int32');
    0060     n = fread(fid, 1, 'int32');
    0061     name = char(name);
    0062     name = deblank(name);
    0063     k = strcmp(name, deblank(varname));
    0064     if(k == 1)
    0065         if type ~= 2
    0066             disp(sprintf('%s is not an interval variable', deblank(varname)));
    0067             return;
    0068         end
    0069         found = 1;
    0070         fseek(fid, offset, 'bof');
    0071         ts_left = fread(fid, [1 n], 'int32');
    0072         ts_right = fread(fid, [1 n], 'int32');
    0073         break
    0074     end
    0075     dummy = fread(fid, 128, 'char');
    0076 end
    0077
    0078 fclose(fid);
    0079
    0080 if found == 0
    0081     disp('did not find variable in the file');
    0082 else
    0083     ts_left = ts_left/freq;
    0084     ts_right = ts_right/freq;
    0085     disp(strcat('number of intervals = ', num2str(n)));
    0086 end