-
Notifications
You must be signed in to change notification settings - Fork 8
/
propval.m
204 lines (175 loc) · 5.88 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
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.
%
%
% License:
%=====================================================================
% This is part of the UNDROIP toolbox, released under
% the GPL. https://github.com/rawi707/UNDROIP/blob/master/LICENSE
%
% The UNDROIP toolbox is available free and
% unsupported to those who might find it useful. We do not
% take any responsibility whatsoever for any problems that
% you have related to the use of the UNDROIP toolbox.
%
% ======================================================================
% 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