source: aedes_readphasetable.m

Last change on this file was 80, checked in by tjniskan, 9 years ago
  • Changed the historical "an2_" prefix to "aedes_" in all files. NOTE:

Any script or function relying to Aedes functions will be broken
because of this. Just do a search/replace from "an2_" to "aedes_" in
your files and all should be well...

  • Changed the name of an2_readtab.m to a more informative

aedes_readphasetable.m

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

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008