# nex_marker

## PURPOSE

nex_marker(filename, varname): Read a marker variable from a .nex file

## SYNOPSIS

function [n, nm, nl, ts, names, m] = nex_marker(filename, varname)

## DESCRIPTION

     nex_marker(filename, varname): Read a marker variable from a .nex file

     [n, nm, nl, ts, names, m] = nex_marker(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 - number of markers
       nm - number of fields in each marker
       nl - number of characters in each marker field
       ts - array of marker timestamps (in seconds)
       names - names of marker fields ([nm 64] character array)
       m - character array of marker values [n nl nm]

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- test_nex

## SOURCE CODE

    0001 function [n, nm, nl, ts, names, m] = nex_marker(filename, varname)
    0002 % nex_marker(filename, varname): Read a marker variable from a .nex file
    0003 %
    0004 % [n, nm, nl, ts, names, m] = nex_marker(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 - number of markers
    0016 %   nm - number of fields in each marker
    0017 %   nl - number of characters in each marker field
    0018 %   ts - array of marker timestamps (in seconds)
    0019 %   names - names of marker fields ([nm 64] character array)
    0020 %   m - character array of marker values [n nl nm]
    0021
    0022 n = 0;
    0023 nm = 0;
    0024 nl = 0;
    0025 ts = 0;
    0026 m = 0;
    0027 names = 0;
    0028
    0029 if(nargin ~= 2)
    0030    disp('2 input arguments are required')
    0031    return
    0032 end
    0033
    0034 if(ischar(filename) == 0)
    0035    disp('input arguments should be character arrays')
    0036    return
    0037 end
    0038
    0039 if(ischar(varname) == 0)
    0040    disp('input arguments should be character arrays')
    0041    return
    0042 end
    0043
    0044 if(isempty(filename))
    0045    [fname, pathname] = uigetfile('*.nex', 'Select a Nex file');
    0046     filename = strcat(pathname, fname);
    0047 end
    0048
    0049 fid = fopen(filename, 'r');
    0050 if(fid == -1)
    0051     disp('cannot open file');
    0052    return
    0053 end
    0054
    0055 disp(strcat('file = ', filename));
    0056 magic = fread(fid, 1, 'int32');
    0057 version = fread(fid, 1, 'int32');
    0058 comment = fread(fid, 256, 'char');
    0059 freq = fread(fid, 1, 'double');
    0060 tbeg = fread(fid, 1, 'int32');
    0061 tend = fread(fid, 1, 'int32');
    0062 nvar = fread(fid, 1, 'int32');
    0063 fseek(fid, 260, 'cof');
    0064 name = zeros(1, 64);
    0065 found = 0;
    0066 for i=1:nvar
    0067     type = fread(fid, 1, 'int32');
    0068     var_version = fread(fid, 1, 'int32');
    0069     name = fread(fid, [1 64], 'char');
    0070     offset = fread(fid, 1, 'int32');
    0071     n = fread(fid, 1, 'int32');
    0072     dummy = fread(fid, 32, 'char');
    0073     adfreq = fread(fid, 1, 'double');
    0074     adtomv = fread(fid, 1, 'double');
    0075     npw = fread(fid, 1, 'int32');
    0076     nm = fread(fid, 1, 'int32');
    0077     nl = fread(fid, 1, 'int32');
    0078     dummy = fread(fid, 68, 'char');
    0079     name = char(name);
    0080     name = deblank(name);
    0081     k = strcmp(name, deblank(varname));
    0082     if(k == 1)
    0083         if type ~= 6
    0084             disp(sprintf('%s is not a marker variable', deblank(varname)));
    0085             return;
    0086         end
    0087         found = 1;
    0088         fseek(fid, offset, 'bof');
    0089         ts = fread(fid, [1 n], 'int32');
    0090         names = zeros(1,64);
    0091         m = zeros(n, nl, nm);
    0092         for j=1:nm
    0093             names(j, :) = fread(fid, [1 64], 'char');
    0094             for p = 1:n
    0095                 m(p, :, j) = fread(fid, [1 nl], 'char');
    0096             end
    0097         end
    0098         break
    0099     end
    0100 end
    0101
    0102 fclose(fid);
    0103
    0104 if found == 0
    0105     disp('did not find variable in the file');
    0106 else
    0107     names = char(names);
    0108     m = char(m);
    0109     ts = ts/freq;
    0110     disp(strcat('number of markers = ', num2str(n)));
    0111 end