-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaedes_readphasetable.m
More file actions
196 lines (165 loc) · 4.29 KB
/
aedes_readphasetable.m
File metadata and controls
196 lines (165 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function [tablib,msg] = aedes_readphasetable(filename)
% AEDES_READPHASETABLE - Read VNMR table file
%
%
% Synopsis:
% [tab,msg]=aedes_readphasetable(filename)
%
% Description:
% The function reads and interprets a Varian phase table file
% "filename" given as a string input argument. The phase order is
% returned in the output argument "tab". The second output
% argument contains the possible error message. If no error
% occurred, the output argument "msg" is empty.
%
% Examples:
% [tab,msg]=aedes_readphasetable('64alt2k');
%
% See also:
% AEDES_READFID, AEDES_READPROCPAR, AEDES
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2006 Juha-Pekka Niskanen <[email protected]>
%
% Department of Physics, Department of Neurobiology
% University of Kuopio, FINLAND
%
% This program may be used under the terms of the GNU General Public
% License version 2.0 as published by the Free Software Foundation
% and appearing in the file LICENSE.TXT included in the packaging of
% this program.
%
% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
msg='';
tablib=[];
%% Open table file
fid=fopen(filename,'r','ieee-be');
if fid<0
tablib=[];
msg=['Could not open file "' filename '" for reading.'];
return
end
%% Scan file
C=textscan(fid,'%s','delimiter','\n');
if isempty(C)
fclose(fid)
tablib=[];
msg=['Error while reading table file "' filename '".'];
return
end
%% Close file
fclose(fid);
tab_str=C{1};
numchars='1234567890';
count_param=1;
count_val=1;
param={};
tablib={};
vals={};
tmp=[];
for ii=1:length(tab_str)
str=deblank(tab_str{ii});
% Skip empty lines
if isempty(str) || all(str==32)
continue;
end
% Check parameter name
ind=find(str=='=');
if isempty(ind)
%tmp(count_val,:)=str2num(str);
tmp=[tmp; l_ParseLine(str)];
%count_val=count_val+1;
else
%% Write buffered values
if ~isempty(param)
vals{end+1}=tmp;
count_val=1;
tmp=[];
end
param{count_param}=str(1:ind-2);
count_param=count_param+1;
% Check if the line contains only the parameter
if ~(str(end)=='=')
%tmp(count_val,:)=str2num(str(ind+1:end));
tmp=[tmp l_ParseLine(str(ind+1:end))];
%count_val=count_val+1;
end
end
end
%% Write buffered values
if ~isempty(param)
vals{end+1}=tmp;
end
%% Construct tablib cell array
tablib=cell(length(param),2);
tablib(:,1)=param;
tablib(:,2)=vals;
%%%%%%%%%%%%%%%%%%%%%
% Parse line
%%%%%%%%%%%%%%%%%%%%%
function out=l_ParseLine(str)
%% Check if possible shorthand indentifiers are found
repall=[];
vals = [];
if any(str=='{') || any(str=='(') || any(str=='[')
ind=find(str=='}');
if ~isempty(ind)
repall=str2num(str(ind+1:end));
str=str(1:ind);
str=strrep(str,'}','');
str=strrep(str,'{','');
end
if ~any(str=='(') && ~any(str=='[')
vals = str2num(str);
else
ind_paren=find((str==')')+(str=='('));
ind_paren=reshape(ind_paren,2,length(ind_paren)/2).';
ind_brackets=find((str==']')+(str=='['));
ind_brackets=reshape(ind_brackets,2,length(ind_brackets)/2).';
for ii=1:length(str)
if any(ii==ind_paren(:,1))
ind=find(ii==ind_paren);
vals = [vals l_ParseParen(str,ind_paren(ind,1),ind_paren(ind,2))];
elseif any(ii==ind_brackets(:,1))
ind=find(ii==ind_brackets);
vals = [vals l_ParseBrackets(str,ind_brackets(ind,1),ind_brackets(ind,2))];
end
end
end
else
out = str2num(str);
return
end
if ~isempty(repall)
tmp=repmat(vals,repall,1);
out = tmp(:).';
else
out = vals;
end
function vals=l_ParseParen(str,ind1,ind2)
%% Parse shorthand parenthesis
% Get multiplier
tmp=find(str(ind2+1:end)==' ');
if isempty(tmp)
mplier = str2num(str(ind2+1:end));
else
mplier = str2num(str(ind2+1:ind2+tmp(1)));
end
% Get values
tmp=str2num(str(ind1+1:ind2-1));
vals=repmat(tmp,1,mplier);
function vals=l_ParseBrackets(str,ind1,ind2)
%% Parse shorthand brackets
% Get multiplier
tmp=find(str(ind2+1:end)==' ');
if isempty(tmp)
mplier = str2num(str(ind2+1:end));
else
mplier = str2num(str(ind2+1:ind2+tmp(1)));
end
% Get values
tmp=str2num(str(ind1+1:ind2-1));
tmp2=repmat(tmp,mplier,1);
vals=tmp2(:).';