41
|
1 |
%======================================================================
|
|
2 |
% L O A D A N T S . M
|
|
3 |
% doc: Thu Jul 21 22:53:21 2011
|
|
4 |
% dlm: Thu Aug 4 10:54:24 2011
|
|
5 |
% (c) 2011 A.M. Thurnherr
|
|
6 |
% uE-Info: 18 71 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
%======================================================================
|
|
8 |
|
|
9 |
% NOTES:
|
|
10 |
% - very restrictive subset of ANTS standard:
|
|
11 |
% - no empty lines
|
|
12 |
% - no in-record comments
|
|
13 |
|
|
14 |
% HISTORY:
|
|
15 |
% Jul 21, 2011: - began working on it
|
|
16 |
% Jul 23, 2011: - made it work
|
|
17 |
% Aug 4, 2011: - replaced "creator" by "Matlab_import"
|
|
18 |
% - BUG: %PARAM names with . were not handled correctly
|
|
19 |
|
|
20 |
function dta_struct = loadANTS(file_name)
|
|
21 |
|
|
22 |
fid = fopen(file_name); % open file
|
|
23 |
if fid < 0
|
|
24 |
error(sprintf('%s: not such file',file_name));
|
|
25 |
end
|
|
26 |
|
|
27 |
dta_struct = struct('Matlab_import',sprintf('loadANTS(''%s'')',file_name));
|
|
28 |
|
|
29 |
l = fgetl(fid); % handle metadata
|
|
30 |
while ischar(l) & regexp(l,'^#')
|
|
31 |
if regexp(l,'^#ANTS#ERROR#')
|
|
32 |
error(l);
|
|
33 |
elseif regexp(l,'^#ANTS#PARAMS#')
|
|
34 |
[tmp,tmp,ptk] = regexp(l,'([\w\.]+){([^}]*)}');
|
|
35 |
for i=1:length(ptk)
|
|
36 |
pname = matlabotomize(token1(i,l,ptk));
|
|
37 |
if sum(size(ptk{i})) < 4 % empty def
|
|
38 |
if isfield(dta_struct,pname)
|
|
39 |
dta_struct = rmfield(dta_struct,pname);
|
|
40 |
end
|
|
41 |
else
|
|
42 |
numval = str2double(token2(i,l,ptk));
|
|
43 |
if isfinite(numval) | strcmpi(token2(i,l,ptk),'nan')
|
|
44 |
dta_struct = setfield(dta_struct,pname,numval);
|
|
45 |
else
|
|
46 |
dta_struct = setfield(dta_struct,pname,token2(i,l,ptk));
|
|
47 |
end
|
|
48 |
end
|
|
49 |
end
|
|
50 |
|
|
51 |
elseif regexp(l,'^#ANTS#FIELDS#')
|
|
52 |
[tmp,tmp,ftk] = regexp(l,'{([^}]*)}');
|
|
53 |
fields = cell(1,length(ftk));
|
|
54 |
for i=1:length(ftk)
|
|
55 |
fields{i} = matlabotomize(token(i,l,ftk));
|
|
56 |
end
|
|
57 |
end % elseif
|
|
58 |
l = fgetl(fid);
|
|
59 |
end % while
|
|
60 |
|
|
61 |
if ischar(l) % not empty file
|
|
62 |
isnumeric = zeros(1,length(fields)); % determine data types
|
|
63 |
[tmp,tmp,tk] = regexp(l,'([^ \t]+)'); % split into tokens
|
|
64 |
for i=1:length(fields)
|
|
65 |
numval = str2double(token(i,l,tk));
|
|
66 |
if isfinite(numval) | strcmpi(token(i,l,tk),'nan')
|
|
67 |
isnumeric(i) = 1;
|
|
68 |
end
|
|
69 |
end
|
|
70 |
end
|
|
71 |
|
|
72 |
dta = cell(length(fields),1); % init data cell array
|
|
73 |
|
|
74 |
while ischar(l) % loop through records
|
|
75 |
[tmp,tmp,tk] = regexp(l,'([^ \t]+)'); % split into tokens
|
|
76 |
for i=1:length(tk)
|
|
77 |
if isnumeric(i)
|
|
78 |
dta{i} = [dta{i} str2double(token(i,l,tk))];
|
|
79 |
else
|
|
80 |
dta{i} = [dta{i} {token(i,l,tk)}];
|
|
81 |
end
|
|
82 |
end
|
|
83 |
l = fgetl(fid);
|
|
84 |
end
|
|
85 |
|
|
86 |
for i=1:length(fields) % copy to structure
|
|
87 |
dta_struct = setfield(dta_struct,fields{i},dta{i});
|
|
88 |
end
|
|
89 |
|
|
90 |
fclose(fid);
|
|
91 |
|
|
92 |
return
|
|
93 |
|
|
94 |
%----------------------------------------------------------------------
|
|
95 |
|
|
96 |
function tk = token(i,str,tki)
|
|
97 |
tk = str(tki{i}(1):tki{i}(2));
|
|
98 |
return;
|
|
99 |
|
|
100 |
function tk = token1(i,str,tki)
|
|
101 |
tk = str(tki{i}(1,1):tki{i}(1,2));
|
|
102 |
return;
|
|
103 |
|
|
104 |
function tk = token2(i,str,tki)
|
|
105 |
tk = str(tki{i}(2,1):tki{i}(2,2));
|
|
106 |
return
|
|
107 |
|
|
108 |
function s = matlabotomize(s)
|
|
109 |
s = strrep(s,'.','_');
|
|
110 |
s = strrep(s,'3','three');
|
|
111 |
|