# nex_wf

## PURPOSE

nex_wf(filename, varname): Read waveform variable from a .nex file

## SYNOPSIS

function [adfreq, n, ts, nf, w] = nex_wf(filename, varname)

## DESCRIPTION

     nex_wf(filename, varname): Read waveform variable from a .nex file

     [adfreq, n, ts, nf, w] = nex_wf(filename, varname)

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

     OUTPUT:
       n - number of waveforms
       ts - array of waveform timestamps (in seconds)
       nf - number of data points in each waveform
       w - matrix of waveform a/d values [n nf] (in millivolts)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- test_nex

## SOURCE CODE

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