0001 function [adfreq, n, ts, fn, d] = nex_cont(filename, varname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
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