--- a/loadANTS.m
+++ b/loadANTS.m
@@ -1,66 +1,111 @@
%======================================================================
% L O A D A N T S . M
% doc: Thu Jul 21 22:53:21 2011
-% dlm: Thu Jul 21 23:43:06 2011
+% dlm: Thu Aug 4 10:54:24 2011
% (c) 2011 A.M. Thurnherr
-% uE-Info: 61 60 NIL 0 0 72 2 2 4 NIL ofnI
+% uE-Info: 18 71 NIL 0 0 72 2 2 4 NIL ofnI
%======================================================================
% NOTES:
-% - very restrictive subset of ANTS standard:
-% = no empty lines
-% - no in-record comments
+% - very restrictive subset of ANTS standard:
+% - no empty lines
+% - no in-record comments
% HISTORY:
-% Jul 21, 2011: - began working on it
+% Jul 21, 2011: - began working on it
+% Jul 23, 2011: - made it work
+% Aug 4, 2011: - replaced "creator" by "Matlab_import"
+% - BUG: %PARAM names with . were not handled correctly
function dta_struct = loadANTS(file_name)
-fid = fopen(file_name); % open file
+fid = fopen(file_name); % open file
if fid < 0
- error(ferror(fid));
+ error(sprintf('%s: not such file',file_name));
end
-dta_struct = struct([]);
+dta_struct = struct('Matlab_import',sprintf('loadANTS(''%s'')',file_name));
-l = fgetl(fid); % handle metadata
+l = fgetl(fid); % handle metadata
while ischar(l) & regexp(l,'^#')
- if regexp(l,'^#ANTS#ERROR#')
- error(l);
- elseif regexp(l,'^#ANTS#PARAMS#')
- params = regexp(l,'(\w+){([^}]*)}','tokens');
- for i=1:length(params)
- if strcmp(params{i}{2},'')
- dta_struct = rmfield(dta_struct,params{i}{1});
+ if regexp(l,'^#ANTS#ERROR#')
+ error(l);
+ elseif regexp(l,'^#ANTS#PARAMS#')
+ [tmp,tmp,ptk] = regexp(l,'([\w\.]+){([^}]*)}');
+ for i=1:length(ptk)
+ pname = matlabotomize(token1(i,l,ptk));
+ if sum(size(ptk{i})) < 4 % empty def
+ if isfield(dta_struct,pname)
+ dta_struct = rmfield(dta_struct,pname);
+ end
else
- dta_struct = setfield(dta_struct,params{i}{1},params{i}{2});
- end % if
- end % for
- elseif regexp(l,'^#ANTS#FIELDS#')
- field = regexp(l,'{([^}]*)}','tokens');
- end % elseif
- l = fgetl(fid);
+ numval = str2double(token2(i,l,ptk));
+ if isfinite(numval) | strcmpi(token2(i,l,ptk),'nan')
+ dta_struct = setfield(dta_struct,pname,numval);
+ else
+ dta_struct = setfield(dta_struct,pname,token2(i,l,ptk));
+ end
+ end
+ end
+
+ elseif regexp(l,'^#ANTS#FIELDS#')
+ [tmp,tmp,ftk] = regexp(l,'{([^}]*)}');
+ fields = cell(1,length(ftk));
+ for i=1:length(ftk)
+ fields{i} = matlabotomize(token(i,l,ftk));
+ end
+ end % elseif
+ l = fgetl(fid);
end % while
-if ~ischar(l) % EOF
- close(fid);
- return
+if ischar(l) % not empty file
+ isnumeric = zeros(1,length(fields)); % determine data types
+ [tmp,tmp,tk] = regexp(l,'([^ \t]+)'); % split into tokens
+ for i=1:length(fields)
+ numval = str2double(token(i,l,tk));
+ if isfinite(numval) | strcmpi(token(i,l,tk),'nan')
+ isnumeric(i) = 1;
+ end
+ end
end
-fmt = ''; % construct scanf format
-for i=1:length(field)
- fmt = [fmt ' %f'];
+dta = cell(length(fields),1); % init data cell array
+
+while ischar(l) % loop through records
+ [tmp,tmp,tk] = regexp(l,'([^ \t]+)'); % split into tokens
+ for i=1:length(tk)
+ if isnumeric(i)
+ dta{i} = [dta{i} str2double(token(i,l,tk))];
+ else
+ dta{i} = [dta{i} {token(i,l,tk)}];
+ end
+ end
+ l = fgetl(fid);
end
-rec1 = sscanf(l,fmt); % split first record
-dta = fscanf(fid,fmt); % read & split remaining records
-
-for i=1:length(field) % copy to structure
- disp(field{i});
- keyboard
- dta_struct = setfield(dta_struct,field{i},[rec1(i); dta(:,i)]);
+for i=1:length(fields) % copy to structure
+ dta_struct = setfield(dta_struct,fields{i},dta{i});
end
-flose(fid);
+fclose(fid);
+
+return
+
+%----------------------------------------------------------------------
+
+function tk = token(i,str,tki)
+ tk = str(tki{i}(1):tki{i}(2));
+ return;
-return
+function tk = token1(i,str,tki)
+ tk = str(tki{i}(1,1):tki{i}(1,2));
+ return;
+
+function tk = token2(i,str,tki)
+ tk = str(tki{i}(2,1):tki{i}(2,2));
+ return
+
+function s = matlabotomize(s)
+ s = strrep(s,'.','_');
+ s = strrep(s,'3','three');
+