-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetInputStructure.m
60 lines (56 loc) · 2.28 KB
/
getInputStructure.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
function [ps, pd] = getInputStructure(parameters)
%GETINPUTSTRUCTURE Get all program inputs as a structure.
%
% getInputStructure(parameters)
%
% parameters can be a JSON string, a ParameterDictionary object, or a file name. If
% neither is found, this function reads a file called
% LastChanceParameters.txt. The file option is only meant to allow the
% program to be tested when a proper calling script is not yet in place.
%
% See also: PARAMETERDICTIONARY
if isa(parameters, 'ParameterDictionary')
ps = parameters.getStruct();
pd = parameters;
else
% If it's not a ParameterDictionary, see if it's a JSON string.
try ps = jsondecode(parameters); %#ok<NASGU>
% That worked as a test, but use the PD constructor
% so inputs are validated.
pd = ParameterDictionary('json', parameters);
disp('Got input values from a JSON string.');
catch ME
if (strcmp(ME.identifier,'MATLAB:json:ExpectedValue'))
% Not a valid JSON string. Try initializing from a file.
fh = fopen(parameters, 'r');
if fh == -1
error('Failed to open file %s', parameters);
end
json = '';
while ~feof(fh)
line = strtrim(fgets(fh));
if ~strncmp(line, '%', 1)
json = strcat(json, line);
end
end
fclose(fh);
fprintf('Decoding %s\n', json);
try ps = jsondecode(json); %#ok<NASGU>
% As above, now repeat with the PD constructor.
pd = ParameterDictionary('json', json);
disp('Got input values from specified file.');
catch ME
disp('Invalid input in specified file!');
rethrow(ME);
end
else
% Some other error. Don't try to handle it here.
rethrow(ME)
end
end
% keyReefs seems to come out of the JSON as a column vector even if it
% goes in as a row. This may be wrong, but for now just force it to be
% what we need.
ps = pd.getStruct();
end
end