1 |
function y = mit_getparm(fname,pname); |
2 |
%function y = mit_getparm(fname,pname); |
3 |
|
4 |
% $Header: $ |
5 |
% $Name: $ |
6 |
|
7 |
y = []; |
8 |
[fp, msg] = fopen(fname,'r'); |
9 |
if fp > 0 |
10 |
str{1} = []; |
11 |
notfound = 0; |
12 |
while isempty(mystrfind(str{1},pname)) |
13 |
str{1} = fgetl(fp); |
14 |
if ~ischar(str{1}); % this catches the end of file (not very clean) |
15 |
notfound = 1; |
16 |
break; |
17 |
end |
18 |
% clear again if commented |
19 |
if ~isempty(mystrfind(str{1},'#')) |
20 |
str{1} = []; |
21 |
end |
22 |
end |
23 |
if notfound |
24 |
disp(['Warning: ' pname ' not found in ' fname]) |
25 |
y=[]; |
26 |
% disp([' setting ' pname ' to zero']) |
27 |
% y = 0; |
28 |
else |
29 |
teststr = []; |
30 |
% find the termination of parameter pname |
31 |
% (next line with an '=' sign |
32 |
% or with a namelist termination character '&' or '/') |
33 |
n = 1; |
34 |
while ( isempty(mystrfind(teststr,'=')) & ... |
35 |
isempty(mystrfind(teststr,'&')) & ... |
36 |
isempty(mystrfind(teststr,'/')) ) |
37 |
n = n + 1; |
38 |
teststr = fgetl(fp); |
39 |
str{n} = teststr; |
40 |
if ~ischar(teststr); break; end |
41 |
end |
42 |
eqind = findstr(str{1},'='); |
43 |
y = str{1}(eqind+1:end-1); |
44 |
% check whether it is a string in quotes or something else |
45 |
quotes = findstr(y,''''); |
46 |
if ~isempty(quotes) |
47 |
y(quotes(2):end) = []; |
48 |
y(1:quotes(1)) = []; |
49 |
else |
50 |
if ~( strcmpi(y,'.TRUE.') | strcmpi(y,'.FALSE.') ) |
51 |
y = convert_str2num(str{1}(eqind+1:end-1)); |
52 |
for k=2:n-1 |
53 |
y = [y; convert_str2num(str{k}(eqind+1:end-1))]; |
54 |
end |
55 |
% $$$ y = str2num(str{1}(eqind+1:end-1))'; |
56 |
% $$$ for k=2:n-1 |
57 |
% $$$ y = [y; str2num(str{k}(eqind+1:end-1))']; |
58 |
% $$$ end |
59 |
end |
60 |
end |
61 |
end |
62 |
fclose(fp); |
63 |
else |
64 |
error([fname ' could not be opened: ' msg]) |
65 |
end |
66 |
|
67 |
return |
68 |
|
69 |
function y = convert_str2num(str) |
70 |
|
71 |
% take care of the n*value format |
72 |
stars = findstr(str,'*'); |
73 |
if isempty(stars) |
74 |
y = str2num(str)'; |
75 |
else |
76 |
if length(stars)==1 |
77 |
y=repmat(str2num(str(stars+1:end)),[str2num(str(1:stars-1)) 1]); |
78 |
else |
79 |
warning('The format n*x1,m*x2, ... cannot be handled') |
80 |
% $$$ for k=1:length(stars) |
81 |
% $$$ % find the beginnin and termination of the set |
82 |
% $$$ end |
83 |
end |
84 |
end |
85 |
|
86 |
return |
87 |
|
88 |
function [ind] = mystrfind(str,pattern) |
89 |
|
90 |
if length(pattern) > length(str) |
91 |
ind = []; |
92 |
return |
93 |
else |
94 |
ind = findstr(str,pattern); |
95 |
end |