-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpropval.m
237 lines (207 loc) · 7.83 KB
/
propval.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function [merged unused] = propval(propvals, defaults, varargin)
% Create a structure combining property-value pairs with default values.
%
% [MERGED UNUSED] = PROPVAL(PROPVALS, DEFAULTS, ...)
%
% Given a cell array or structure of property-value pairs
% (i.e. from VARARGIN or a structure of parameters), PROPVAL will
% merge the user specified values with those specified in the
% DEFAULTS structure and return the result in the structure
% MERGED. Any user specified values that were not listed in
% DEFAULTS are output as property-value arguments in the cell array
% UNUSED. STRICT is disabled in this mode.
%
% ALTERNATIVE USAGE:
%
% [ ARGS ] = PROPVAL(PROPVALS, DEFAULTS, ...)
%
% In this case, propval will assume that no user specified
% properties are meant to be "picked up" and STRICT mode will be enforced.
%
% ARGUMENTS:
%
% PROPVALS - Either a cell array of property-value pairs
% (i.e. {'Property', Value, ...}) or a structure of equivalent form
% (i.e. struct.Property = Value), to be merged with the values in
% DEFAULTS.
%
% DEFAULTS - A structure where field names correspond to the
% default value for any properties in PROPVALS.
%
% OPTIONAL ARGUMENTS:
%
% STRICT (default = true) - Use strict guidelines when processing
% the property value pairs. This will warn the user if an empty
% DEFAULTS structure is passed in or if there are properties in
% PROPVALS for which no default was provided.
%
% EXAMPLES:
%
% Simple function with two optional numerical parameters:
%
% function [result] = myfunc(data, varargin)
%
% defaults.X = 5;
% defaults.Y = 10;
%
% args = propvals(varargin, defaults)
%
% data = data * Y / X;
%
% >> myfunc(data)
% This will run myfunc with X=5, Y=10 on the variable 'data'.
%
% >> myfunc(data, 'X', 0)
% This will run myfunc with X=0, Y=10 (thus giving a
% divide-by-zero error)
%
% >> myfunc(data, 'foo', 'bar') will run myfunc with X=5, Y=10, and
% PROPVAL will give a warning that 'foo' has no default value,
% since STRICT is true by default.
%
% ======================================================================
% Copyright (c) 2012 David Weiss
%
% Permission is hereby granted, free of charge, to any person obtaining
% a copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including
% without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Software, and to
% permit persons to whom the Software is furnished to do so, subject to
% the following conditions:
%
% The above copyright notice and this permission notice shall be
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
% LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% ======================================================================
% ======================================================================
% Copyright (c) 2012 David Weiss
%
% Permission is hereby granted, free of charge, to any person obtaining
% a copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including
% without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Software, and to
% permit persons to whom the Software is furnished to do so, subject to
% the following conditions:
%
% The above copyright notice and this permission notice shall be
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
% LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% ======================================================================
% Backwards compatibility
pvdef.ignore_missing_default = false;
pvdef.ignore_empty_defaults = false;
% check for the number of outputs
if nargout == 2
pvdef.strict = false;
else
pvdef.strict = true;
end
pvargs = pvdef;
% Recursively process the propval optional arguments (possible
% because we only recurse if optional parameters are given)
if ~isempty(varargin)
pvargs = propval(varargin, pvdef);
end
% NOTE: Backwards compatibility with previous version of propval
if pvargs.ignore_missing_default | pvargs.ignore_empty_defaults
pvargs.strict = false;
end
% check for a single cell argument; assume propvals is that argument
if iscell(propvals) && numel(propvals) == 1
propvals = propvals{1};
end
% check for valid inputs
if ~iscell(propvals) & ~isstruct(propvals)
error('Property-value pairs must be a cell array or a structure.');
end
if ~isstruct(defaults) & ~isempty(defaults)
error('Defaults struct must be a structure.');
end
% check for empty defaults structure
if isempty(defaults)
if pvargs.strict & ~pvargs.ignore_missing_default
error('Empty defaults structure passed to propval.');
end
defaults = struct();
end
defaultnames = fieldnames(defaults);
defaultvalues = struct2cell(defaults);
% prepare the defaults structure, but also prepare casechecking
% structure with all case stripped
defaults = struct();
casecheck = struct();
for i = 1:numel(defaultnames)
defaults.(defaultnames{i}) = defaultvalues{i};
casecheck.(lower(defaultnames{i})) = defaultvalues{i};
end
% merged starts with the default values
merged = defaults;
unused = {};
used = struct();
properties = [];
values = [];
% To extract property value pairs, we use different methods
% depending on how they were passed in
if isstruct(propvals)
properties = fieldnames(propvals);
values = struct2cell(propvals);
else
properties = { propvals{1:2:end} };
values = { propvals{2:2:end} };
end
if numel(properties) ~= numel(values)
error(sprintf('Found %g properties but only %g values.', numel(properties), ...
numel(values)));
end
% merge new properties with defaults
for i = 1:numel(properties)
if ~ischar(properties{i})
error(sprintf('Property %g is not a string.', i));
end
% convert property names to lower case
properties{i} = properties{i};
% check for multiple usage
if isfield(used, properties{i})
error(sprintf('Property %s is defined more than once.\n', ...
properties{i}));
end
% Check for case errors
if isfield(casecheck, lower(properties{i})) & ...
~isfield(merged, properties{i})
error(['Property ''%s'' is equal to a default property except ' ...
'for case.'], properties{i});
end
% Merge with defaults
if isfield(merged, properties{i})
merged.(properties{i}) = values{i};
else
% add to unused property value pairs
unused{end+1} = properties{i};
unused{end+1} = values{i};
% add to defaults, just in case, if the user isn't picking up "unused"
if (nargout == 1 & ~pvargs.strict)
merged.(properties{i}) = values{i};
end
if pvargs.strict
error('Property ''%s'' has no default value.', properties{i});
end
end
% mark as used
%used.(properties{i}) = true;
end