-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaedes_readprocpar.m
More file actions
176 lines (156 loc) · 4.91 KB
/
Copy pathaedes_readprocpar.m
File metadata and controls
176 lines (156 loc) · 4.91 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
function [procpar,msg] = aedes_readprocpar(filename)
% AEDES_READPROCPAR - Read VNMR (Varian) PROCPAR-file
%
%
% Synopsis:
% [procpar,msg]=aedes_readprocpar(filename)
%
% Description:
% The function reads the VNMR parameter file and returns a
% procpar-structure with parameters as structure fields. The input
% argument is a string containing either the full path to the
% procpar file or the .fid-directory containing the procpar file.
%
% If an error occurs, the first output argument will be empty
% (i.e. procpar=[];) and the second output argument msg will
% contain the error message. Msg is an empty string if no error has
% occurred.
%
% NOTE: AEDES_READPROCPAR adds an additional array field in the
% procpar-structure. The parameter procpar.arrayjoint does NOT
% exist in the original procpar-file. The arrayjoint field contains
% only a flag that can be used to determine if multiparameter
% arrayed acquisition is arrayed jointly (1=parameters arrayed
% jointly, 0=parameters not arrayed jointly).
%
% Examples:
% procpar=aedes_readprocpar('C:\MyData\MyAcquisition.fid\procpar')
%
% or
%
% procpar=aedes_readprocpar('C:\MyData\MyAcquisition.fid')
%
% See also:
% AEDES_READFID, AEDES_DATA_READ, AEDES, AEDES_READTAB
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
%
% 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.
% Default values for output arguments
procpar=[];
msg='';
%% Ask for a file if not given as an input argument
if nargin==0
[fn,fp,fi]=uigetfile({'PROCPAR;procpar','Varian PROCPAR files';...
'*.*','All Files (*.*)'},...
'Select PROCPAR file');
if all(fn==0) | all(fp==0)
% Cancel pressed
return
end
filename = [fp,fn];
end
% Parse filename
[fpath,fname,fext]=fileparts(filename);
if ~strcmpi(fname,'procpar')
if isempty(fname)
fpath = [fpath,filesep];
else
fpath = [fpath,filesep,fname,fext,filesep];
end
fname = 'procpar';
else
fpath = [fpath,filesep];
end
%% Open file
fid=fopen([fpath,fname],'r','ieee-be');
if fid<0
msg=['Could not open procpar file "',fpath,fname,'" for reading'];
return
end
%% Read whole file into cell array
try
try
C = textscan(fid,'%s','delimiter','\n','BufSize',1024*1024);
catch
% Bufsize returns an error in R2014b->
C = textscan(fid,'%s','delimiter','\n');
end
procpar_str=C{1};
catch
msg={'Error while reading procpar file.','',...
lasterr};
fclose(fid);
return
end
%% Close file
fclose(fid);
%% Parse procpar file lines
%try
nonlabelchars='123456789';
field_chars = '_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
nonfield_chars = char(setdiff(0:256,double(field_chars)));
for ii=1:length(procpar_str)
if any(procpar_str{ii}(1)==nonlabelchars)
%% Read info
str=deblank(procpar_str{ii});
ind=find(str==' ');
if isempty(ind)
ind=0;
end
str=str(ind(1)+1:end);
if str(1)=='"'
ind2=find(str=='"');
for kk=1:2:(length(ind2))
procpar.(label){end+1} = strrep(str(ind2(kk):ind2(kk+1)),'"','');
end
else
procpar.(label) = str2num(str);
end
elseif procpar_str{ii}(1)=='"' % Read string from line
str=deblank(procpar_str{ii});
procpar.(label){end+1} = strrep(str,'"','');
elseif procpar_str{ii}(1)=='0' % Empty line, end of block
continue
else
%% Read label
ind=find(procpar_str{ii}==' ',1,'first');
label=procpar_str{ii}(1:ind-1);
%% Make sure that the characters in the label are compatible with
%% Matlab structure fields. If not, replace them with underscore...
ind2=ismember(label,nonfield_chars);
if any(ind2)
label(ind2)='_';
end
procpar.(label)={};
end
end
%% Parse array parameter. If more than one parameters are arrayed, they
% are separated with commas. If parameters are jointly arrayed they
% are enclosed in brackets.
if isfield(procpar,'array') && ~isempty(procpar.array{1})
str = procpar.array{1};
% Add Matlab cell characters
str = strrep(strrep(str,'(','{'),')','}');
str = ['{',str,'}'];
% Add string characters around words
str=regexprep(str,'(\w+)(,|\})','''$1''$2');
% Evaluate to formulate s cell
procpar.array = eval(str);
end
%catch
% msg=['Error while parsing procpar lines'];
% procpar=[];
% return
%end