comparison m-toolbox/classes/@smodel/subs.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % SUBS substitutes symbolic parameters with the given values.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SUBS substitutes symbolic parameters with the given values.
5 %
6 % CALL: mdl = mdl.subs(pl)
7 % mdl = mdl.subs('all') % Substitutes all parameters
8 % mdl = mdl.subs('a', 'b') % Substitutes the parameters 'a' and
9 % 'b' with their default values
10 % mdl = mdl.subs({'a', 'b'}) % Substitutes the parameters 'a' and
11 % 'b' with their default values
12 % mdl = mdl.subs('a', 33) % Substitutes the parameters 'a'
13 % with 33
14 % mdl = mdl.subs({'a'}, {33}) % Substitutes the parameters 'a'
15 % with 33
16 %
17 % Examples
18 % --------
19 %
20 % 1) m = subs(m, plist('Params', 'all')) % substitute all default values
21 % 2) m = subs(m, plist('Params', {'a', 'b'}, 'Values',{1, 1:10})) % substitute
22 % values
23 % 3) m = subs(m, plist('Params', {'a', 'b'})) % substitute default values
24 %
25 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'subs')">Parameters Description</a>
26 %
27 % VERSION: $Id: subs.m,v 1.11 2011/04/08 08:56:28 hewitson Exp $
28 %
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30
31 function varargout = subs(varargin)
32
33 %%% Check if this is a call for parameters
34 if utils.helper.isinfocall(varargin{:})
35 varargout{1} = getInfo(varargin{3});
36 return
37 end
38
39 % Collect input variable names
40 in_names = cell(size(varargin));
41 for ii = 1:nargin,in_names{ii} = inputname(ii);end
42
43 % Collect all AOs and plists
44 [mdls, mdl_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
45 [pls, pl_invars, rest] = utils.helper.collect_objects(rest, 'plist', in_names);
46
47 % combine plists
48 pl = combine(pls, getDefaultPlist());
49
50 % Check if the user uses a plist or not
51 if isempty(pls) && ~isempty(rest)
52 % The user doesn't use a input plist
53 iparams = {};
54 ivals = {};
55 iexceptions = {};
56 % Collect each character or cellstr in 'rest' as a parameter
57 % Collect each integer of numeric cell
58 for ii=1:numel(rest)
59 if ischar(rest{ii}) && strcmp(rest{ii}, 'all') && ii == 1
60 iparams = 'all';
61 break;
62 elseif ischar(rest{ii})
63 iparams = [iparams rest{ii}];
64 elseif iscellstr(rest{ii})
65 iparams = [iparams rest{ii}];
66 elseif isnumeric(rest{ii})
67 ivals = [ivals rest{ii}];
68 elseif iscell(rest{ii})
69 c = rest{ii};
70 for kk=1:numel(c)
71 if isnumeric(c{kk}), ivals = [ivals c{kk}]; end
72 end
73 end
74 end
75 pl.pset('params', iparams);
76 pl.pset('values', ivals);
77
78 else
79 % The user uses a input plist
80 iparams = find(pl, 'params');
81 ivals = find(pl, 'values');
82 iexceptions = find(pl, 'exceptions');
83 end
84
85 %
86
87 % Loop over input models
88 bs = copy(mdls, nargout);
89 for j=1:numel(mdls)
90
91 mdl = bs(j);
92
93 if ischar(iparams)
94 if strcmpi(iparams, 'all')
95 iparams = mdl.params;
96 ivals = mdl.values;
97 else
98 iparams = {iparams};
99 end
100 end
101
102 if isempty(ivals)
103 ivals = {};
104 % get values from the model
105 for ll=1:numel(iparams)
106 idx = find(strcmp(iparams{ll}, mdl.params));
107 ivals = [ivals mdl.values(idx)];
108 end
109 end
110
111 % remove the exceptions from the params list
112 if ~isempty(iexceptions)
113 idx = ismember(iparams, iexceptions);
114 iparams = iparams(~idx);
115 ivals = ivals(~idx);
116 end
117
118 if isempty(ivals) && ~strcmpi(iparams, 'all')
119 error('### Please specify one value per parameter to substitute.');
120 end
121 if ~ischar(iparams) && numel(iparams) ~= numel(ivals)
122 error('### Please specify one value per parameter to substitute.');
123 end
124
125 if ~iscell(ivals)
126 ivals = {ivals};
127 end
128
129 if numel(iparams) ~= numel(ivals)
130 error('### The number of parameters and values doesn''t match for model %s', mdl.name);
131 end
132
133 % Get remaining parameters and values for the output model
134 oparams = {};
135 ovals = {};
136 for kk=1:numel(mdl.params)
137 if ~utils.helper.ismember(mdl.params{kk}, iparams)
138 oparams = [oparams mdl.params(kk)];
139 if ~isempty(mdl.values)
140 ovals = [ovals mdl.values(kk)];
141 end
142 end
143 end
144
145 if ~isempty(iparams) && ~isnumeric(mdl.expr)
146 mdl.expr = subs(mdl.expr, iparams, ivals);
147 mdl.values = ovals;
148 mdl.params = oparams;
149 % add history
150 mdl.addHistory(getInfo('None'), pl, mdl_invars(j), mdls(j).hist);
151 end
152
153 end
154
155 % Set outputs
156 varargout{1} = bs;
157 end
158
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % Local Functions %
161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 %
165 % FUNCTION: getInfo
166 %
167 % DESCRIPTION: Get Info Object
168 %
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170
171 function ii = getInfo(varargin)
172 if nargin == 1 && strcmpi(varargin{1}, 'None')
173 sets = {};
174 pl = [];
175 else
176 sets = {'Default'};
177 pl = getDefaultPlist;
178 end
179 % Build info object
180 ii = minfo(mfilename, 'smodel', 'ltpda', utils.const.categories.helper, '$Id: subs.m,v 1.11 2011/04/08 08:56:28 hewitson Exp $', sets, pl);
181 end
182
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 %
185 % FUNCTION: getDefaultPlist
186 %
187 % DESCRIPTION: Get Default Plist
188 %
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190
191 function plout = getDefaultPlist()
192 persistent pl;
193 if exist('pl', 'var')==0 || isempty(pl)
194 pl = buildplist();
195 end
196 plout = pl;
197 end
198
199 function pl = buildplist()
200
201 pl = plist();
202
203 % Params
204 p = param({'Params', 'The parameters to substitute for.<br>Specify ''all'' to substitute all.'}, {1, {'all'}, paramValue.OPTIONAL});
205 pl.append(p);
206
207 % Values
208 p = param({'values', 'A cell-array of values to set that overide the defaults.'}, {1, {{}}, paramValue.OPTIONAL});
209 pl.append(p);
210
211 % Exceptions
212 p = param({'exceptions', 'A cell-array of parameters which will not be substitute.'}, paramValue.EMPTY_CELL);
213 pl.append(p);
214
215 end
216
217 %
218 % PARAMETERS: 'Params' - A cell array of the parameter names to
219 % substitute. If you specify 'all' for this
220 % parameter, then the current values of mdl.params
221 % will be substituted.
222 % 'Values' - The values to substitute.
223 %