0001 function [n, nm, nl, ts, names, m] = nex_marker(filename, varname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
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