# nex_cont

## PURPOSE

nex_cont(filename, varname): Read continuous variable from a .nex file

## SYNOPSIS

function [adfreq, n, ts, fn, d] = nex_cont(filename, varname)

## DESCRIPTION

     nex_cont(filename, varname): Read continuous variable from a .nex file

     [adfreq, n, ts, fn, d] = nex_cont(filename, varname)

     INPUT:
       filename - if empty string, will use File Open dialog
       varname - variable name

               continuous (a/d) data come in fragments. Each fragment has a timestamp
               and a number of a/d data points. The timestamp corresponds to
               the time of recording of the first a/d value in this fragment.
               All the data values stored in the vector d.
     OUTPUT:
       n - total number of data points
       ts - array of fragment timestamps (one timestamp for fragment, in seconds)
       fn - number of data points in each fragment
       d - array of a/d values (in millivolts)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- test_nex

## SOURCE CODE

    0001 function [adfreq, n, ts, fn, d] = nex_cont(filename, varname)
    0002 % nex_cont(filename, varname): Read continuous variable from a .nex file
    0003 %
    0004 % [adfreq, n, ts, fn, d] = nex_cont(filename, varname)
    0005 %
    0006 % INPUT:
    0007 %   filename - if empty string, will use File Open dialog
    0008 %   varname - variable name
    0009 %
    0010 %           continuous (a/d) data come in fragments. Each fragment has a timestamp
    0011 %           and a number of a/d data points. The timestamp corresponds to
    0012 %           the time of recording of the first a/d value in this fragment.
    0013 %           All the data values stored in the vector d.
    0014 % OUTPUT:
    0015 %   n - total number of data points
    0016 %   ts - array of fragment timestamps (one timestamp for fragment, in seconds)
    0017 %   fn - number of data points in each fragment
    0018 %   d - array of a/d values (in millivolts)
    0019
    0020 n = 0;
    0021 adfreq = 0;
    0022 ts = 0;
    0023 fn = 0;
    0024 d = 0;
    0025
    0026 if(nargin ~= 2)
    0027    disp('2 input arguments are required')
    0028    return
    0029 end
    0030
    0031 if(ischar(filename) == 0)
    0032    disp('input arguments should be character arrays')
    0033    return
    0034 end
    0035
    0036 if(ischar(varname) == 0)
    0037    disp('input arguments should be character arrays')
    0038    return
    0039 end
    0040
    0041 if(isempty(filename))
    0042    [fname, pathname] = uigetfile('*.nex', 'Select a Nex file');
    0043     filename = strcat(pathname, fname);
    0044 end
    0045
    0046 fid = fopen(filename, 'r');
    0047 if(fid == 0)
    0048    return
    0049 end
    0050
    0051 disp(strcat('file = ', filename));
    0052 magic = fread(fid, 1, 'int32');
    0053 version = fread(fid, 1, 'int32');
    0054 comment = fread(fid, 256, 'char');
    0055 freq = fread(fid, 1, 'double');
    0056 tbeg = fread(fid, 1, 'int32');
    0057 tend = fread(fid, 1, 'int32');
    0058 nvar = fread(fid, 1, 'int32');
    0059 fseek(fid, 260, 'cof');
    0060 name = zeros(1, 64);
    0061 found = 0;
    0062 for i=1:nvar
    0063     type = fread(fid, 1, 'int32');
    0064     var_version = fread(fid, 1, 'int32');
    0065     name = fread(fid, [1 64], 'char');
    0066     offset = fread(fid, 1, 'int32');
    0067     nf = fread(fid, 1, 'int32');
    0068     dummy = fread(fid, 32, 'char');
    0069     adfreq = fread(fid, 1, 'double');
    0070     adtomv = fread(fid, 1, 'double');
    0071     n = fread(fid, 1, 'int32');
    0072     name = char(name);
    0073     name = deblank(name);
    0074     k = strcmp(name, deblank(varname));
    0075     if(k == 1)
    0076         if type ~= 5
    0077             disp(sprintf('%s is not a continuous variable', deblank(varname)));
    0078             return;
    0079         end
    0080         found = 1;
    0081         fseek(fid, offset, 'bof');
    0082         ts = fread(fid, [1 nf], 'int32');
    0083         fn = fread(fid, [1 nf], 'int32');
    0084         d = fread(fid, [1 n], 'int16');
    0085         break
    0086     end
    0087     dummy = fread(fid, 76, 'char');
    0088 end
    0089
    0090 fclose(fid);
    0091
    0092 if found == 0
    0093     disp('did not find variable in the file');
    0094 else
    0095     ts = ts/freq;
    0096     d = d*adtomv;
    0097     fn(nf+1) = n;
    0098     fn = diff(fn);
    0099     disp(strcat('number of data points = ', num2str(n)));
    0100 end