-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathassignopts.m
46 lines (43 loc) · 1.11 KB
/
assignopts.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
function assignopts(opts)
%ASSIGNOPTS Assign options.
% ASSIGNOPTS(OPTS) assigns the options in OPTS. This is useful
% whenever you prefer to use 'name' rather than 'OPTS.name'
% throughout the code. OPTS can either be a structure or a
% cell-vector with property/value pairs.
%
% Example:
% % caller's site:
% opts.order = 2;
%
% % inside fun(opts, ...)
% def.rho = 0.01;
% def.method = 'lagrange';
% def.order = 4;
% opts = parseopts(def,opts);
% assignopts(opts);
%
% % use the variables directly
% order % note: 2
% method
%
% See also PARSEOPTS, ASSIGNIN.
% S. Engblom 2010-08-25
if isstruct(opts)
if any(size(opts) ~= 1)
error('Expecting a scalar structure.');
end
field = fieldnames(opts);
val = struct2cell(opts);
elseif iscell(opts)
field = reshape(opts(1:2:end),[],1);
val = reshape(opts(2:2:end),[],1);
if size(field,1) ~= size(val,1)
error('Cell-vector must contain property/value pairs.');
end
else
error('Options must either be a struct or a cell-vector.');
end
% immediate
for i = 1:size(field,1)
assignin('caller',field{i},val{i});
end