-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetopts.m
More file actions
132 lines (124 loc) · 4.2 KB
/
setopts.m
File metadata and controls
132 lines (124 loc) · 4.2 KB
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
%setfields Add Field-Value pairs to structure
%
%S = setfields(S0,'field1',value1,'field2',value2,...)
%S = setfields(S0,{'field1',value1,'field2',value2,...})
%S = setfields(S0,'{'field1','field2',...},{value1,value2,...})
%S = setfields(S0,S1)
% where S1 = struct('field1',value1,'field2',value2,...)
%
%sets the field-value pairs in the structure S0, and errors if the fields
%do not exist. The values value1, value2 etc. will be assigned to the
%fields field1, field2, etc in the structure S0, and returned as S. The
%field-value pairs may be specified as arguments, cell arrays, or a
%structure
%
%Modification History
%Created 2011.05.19 Peter Hollender
% 2014.04.16 Added recursive structure adding
%
% See also fieldnames getfield setfield rmfield orderfields
function [s o] = setopts(s0,varargin)
s = s0;
if nargin == 1 % Pass through
o = {};
elseif nargin == 2 && isstruct(varargin{1}) % Structure
s1 = varargin{1};
o = s1;
tgtfields = fieldnames(s1);
for i = 1:length(tgtfields)
tgtfield = tgtfields{i};
[s, oi] = setopts(s,tgtfield,s1.(tgtfield));
if isempty(oi)
o = rmfield(o,tgtfield);
else
o = o;
%o.(tgtfield) = addfields(struct,oi);
end
end
elseif nargin == 3 && iscell(varargin{1}) && iscell(varargin{2}) % Cell of parameters, cell of values
tgtfields = varargin{1};
values = varargin{2};
o = {};
for i = 1:length(tgtfields)
tgtfield = tgtfields{i};
[s oi] = setopts(s,tgtfield,values{i});
if ~isempty(oi)
o = [o oi];
end
end
elseif nargin == 2 && iscell(varargin{1}) && length(varargin{1})==1 %Cell of other inputs
[s, o] = setopts(s,varargin{1}{1});
elseif nargin == 2 && iscell(varargin{1}) && mod(length(varargin{1}),2) == 0 %Cell {P1,V1,P2,V2...}
tgtfields = varargin{1}(1:2:end);
values = varargin{1}(2:2:end);
o = {};
for i = 1:length(tgtfields)
tgtfield = tgtfields{i};
[s, oi] = setopts(s,tgtfield,values{i});
if ~isempty(oi)
o = [o oi];
end
end
elseif mod(nargin,2) % P1,V1,P2,V2,...
tgtfields = varargin(1:2:end);
values = varargin(2:2:end);
o = {};
for i = 1:length(tgtfields)
tgtfield = tgtfields{i};
dots = strfind(tgtfield,'.');
if isempty(dots)
[s oi] = caseInsensitiveAdd(s,tgtfield,values{i});
if ~isempty(oi)
if isstruct(oi)
o = addfields(o,oi);
else
o = [o oi];
end
end
else
subfield = tgtfield(dots(1)+1:end);
tgtfield = tgtfield(1:dots(1)-1);
fields = fieldnames(s);
matchi = find(strcmpi(fields,tgtfield));
match = find(strcmp(fields,tgtfield));
if isempty(match) && ~isempty(matchi)
warning(sprintf('Case-Insensitive Match for structure ''%s'' found. Assigning value specified for ''%s'' to ''%s''',fields{matchi},tgtfield,fields{matchi}));
tgtfield = fields{matchi};
end
if ~isfield(s,tgtfield)
oi = {[tgtfield '.' subfield],values{i}};
o = [o oi];
else
[s.(tgtfield) oi] = setopts(s.(tgtfield),subfield,values{i});
if ~isempty(oi)
oi{1} = [tgtfield '.' subfield];
o = [o oi];
end
end
end
end
else %Invalid Syntax
error('Unable to parse field-value pairs')
end
end
function [s o]= caseInsensitiveAdd(s0,tgtfield,value)
s = s0;
fields = fieldnames(s0);
match = find(strcmp(fields,tgtfield));
matchi = find(strcmpi(fields,tgtfield));
if isempty(matchi)
o = {tgtfield, value};
%error('Field ''%s'' not found',tgtfield);
%s.(tgtfield) = value;
else
o = {};
if isempty(match)
warning(sprintf('Case-Insensitive Match for option ''%s'' found. Assigning value specified for ''%s'' to ''%s''',fields{matchi},tgtfield,fields{matchi}));
end
if isstruct(value) && isstruct(s.(fields{matchi}))
[s.(fields{matchi}) o] = setopts(s.(fields{matchi}),value);
else
s.(fields{matchi}) = value;
end
end
end