-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_params.m
87 lines (77 loc) · 2.8 KB
/
load_params.m
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
% Replace default values of parameters with custom values from an m file.
% 'vars' is a cell array of strings, each of which is the name of a variable that are global and can be
% updated.
function load_params(params_file, vars)
% Make input args consistent
if ischar(vars)
v = vars;
clear vars;
vars{1} = v;
end
if ~exist(strcat(params_file, '.m'), 'file')
warning('Parameters file ''%s'' does not exist.', strcat(params_file, '.m'));
return;
end
% Check that each variable to be updated is, in fact, available in the
% global workspace. If not, warn. If so, make it accessible in this
% function.
for i = 1:length(vars)
a = who('global', vars{i});
if isempty(a)
warning('Global variable ''%s'' undefined, therefore ignored.', vars{i});
else
eval(sprintf('global %s;', vars{i}));
end
end
% This will create a copy of the top-level variables in the params file
new_params = load_the_damn_thing(params_file);
% textscan reads one line at a time, causing difficulty for multi-line
% variable declarations (e.g. nicely-typeset arrays). So we use this
% just to get the variable names to check their legitimacy.
fid = fopen(strcat(params_file, '.m'), 'r');
disp(sprintf('======= Loading parameters from ''%s''=======', params_file));
while true
% Read a line
s = fgetl(fid);
if ~ischar(s)
break;
end
% Chew off anything comment-y
n = findstr(s, '%');
if n == 1
continue;
elseif ~isempty(n)
s = s(1:n-1);
end
% See if the line contains an equals sign
n = findstr(s, '=');
if isempty(n) | n == 1
continue;
else
s = s(1:n-1);
end
% Try to read the value of the variable. If it doesn't exist, catch
% the error and turn it into a warning.
try
eval(sprintf('thing = %s;', s));
disp(sprintf('%s = new_params.%s;', s, s));
eval(sprintf('%s = new_params.%s;', s, s));
catch ME
warning('Parameter ''%s'' undefined by PrintImage, therefore ignored!', s);
end
end
disp('==================================================');
end
% Namespaces: load the vars into this function's workspace, and then prefix
% them so they can be passed back without overwriting the real global and
% hence losing the information on whether the variable is legit.
function [ out ] = load_the_damn_thing(params_file)
eval(params_file);
v = whos;
for i = 1:length(v)
if strcmp(v(i).name, 'params_file')
continue;
end
eval(sprintf('out.%s = %s;', v(i).name, v(i).name));
end
end