forked from acsutt0n/Drosophila-larvae_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetOptions.m
209 lines (180 loc) · 5.01 KB
/
GetOptions.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
function [options, varargout] = GetOptions(defaults, args, allowExtra)
% [options, modified] = GetOptions(defaults, args)
% Create a structure of options from defaults and user-overrides.
% INPUTS:
% -defaults: a cell array with keyword, value ordering
% or a structure
% -args: either a structure or
% a cell array
% if a cell array, user can pass ALL keywords or
% just go by input order.
% if args is ommitted or length zero, the options will be the defaults.
% OPTIONAL
% -allowExtra false
% if set to true, ignore options specified in args that don't correspond
% to anything in defaults.
% OUTPUTS:
% -options: structure with fields set to their proper values
if nargin < 1 || nargin > 3
help GetOptions
error('Invalid number of inputs.')
end
if ~iscell(defaults) && ~isstruct(defaults)
help GetOptions
error('"defaults" must be a cell array or structure.')
end
if iscell(defaults);
try
defaults = cellOptionsToStruct(defaults);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
end
fNames = fieldnames(defaults);
modified = struct();
for n = 1:length(fNames)
modified.(fNames{n}) = false;
end
if nargin == 1 || isempty(args)
options = defaults;
varargout = {modified};
return
end
if nargin < 3
allowExtra = false;
end
if iscell(args) && length(args) == 1 && isstruct(args{1})
args = args{1};
end
if isstruct(args)
% user passed a struct containing options
try
[options, modified] = ...
getStructOptions(defaults, modified, args, allowExtra);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
elseif iscell(args)
% user passed a cell array containing options
try
[options, modified] = ...
getCellArrOptions(defaults, modified, args, allowExtra);
catch err
% get help for the m-file that called GetOptions
getHelp()
% rethrow error
rethrow(err)
end
else
help GetOptions
error('"args" must be a structure or a cell array.')
end
varargout = {modified};
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function optStruct = cellOptionsToStruct(cellOpts)
numOpts = length(cellOpts);
if mod(numOpts, 2) ~= 0
error('options must come in "key" "value" pairs.')
end
numOpts = numOpts / 2;
for n = 1:numOpts
mVal = 2 * n;
mKey = mVal - 1;
key = cellOpts{mKey};
val = cellOpts{mVal};
if ~ischar(key)
error('Invalid option key: %s', key)
end
optStruct.(key) = val;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [options, modified] = ...
getStructOptions(defaults, modified, argStruct, allowExtra)
% get options as defaults with overrides specified by argStruct
options = defaults;
fNames = fieldnames(argStruct);
for n = 1:length(fNames)
field_n = fNames{n};
value_n = argStruct.(field_n);
if isempty(value_n)
% skip empty values
continue
end
if isfield(options, field_n) || allowExtra
% this is a valid field, override the default
% or
% save extra field (maybe another program will want it)
options.(field_n) = value_n;
modified.(field_n) = true;
else
error('Invalid option: %s', field_n)
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [options, modified] = ...
getCellArrOptions(defaults, modified, args, allowExtra)
% get options as default with overrides specified by cell array args
fNames = fieldnames(defaults);
keys = args(1:2:end);
options = defaults;
if mod(length(args), 2) == 0 && iscellstr(keys) && ...
(~isempty(intersect(fNames, keys)) || length(args) > length(fNames))
% overrides are keyword, value pairs
if ~allowExtra && length(keys) > length(fNames)
error('Too many arguments')
end
values = args(2:2:end);
for n = 1:length(keys)
if isempty(values{n})
% skip empty values
continue
end
if isfield(defaults, keys{n}) || allowExtra
% this is a valid field, override the default
% or
% save extra field (maybe another program will want it)
options.(keys{n}) = values{n};
modified.(keys{n}) = true;
else
error('Invalid option: %s', keys{n});
end
end
else
% overrides are just a list of values
if length(args) > length(fNames)
error('Too many arguments')
end
for n = 1:length(args)
options.(fNames{n}) = args{n};
modified.(fNames{n}) = true;
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function getHelp()
% get help for the m-file that called GetOptions
st = dbstack;
thisFile = mfilename;
for n = 1:length(st)
if ~strcmp(st(n).file, thisFile)
%this is the calling file
callFile = st(n).file;
ind = find(callFile == filesep, 1, 'last');
if ~isempty(ind)
callFile = callFile((ind+1):end);
end
help(callFile)
return
end
end
help GetOptions
return