comparison m-toolbox/classes/@smodel/setParameters.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 % SETPARAMETERS Set some parameters to the symbolic model (smodel) object
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SETPARAMETERS Set some parameters to the symbolic model
5 % (smodel) object. It is possible to set only the parameter
6 % name or to set the parameter names with their values.
7 % Exist the parameter-name in the object then replace this
8 % method the existing value.
9 %
10 % CALL: obj = obj.setParameters(key-value pairs);
11 % obj = obj.setParameters(plist);
12 % obj = obj.setParameters(two cell-arrays);
13 % obj = obj.setParameters(single cell-array of names);
14 %
15 % INPUTS: obj - a ltpda smodel object.
16 % key-value - A single key-value pair or a list of key-value
17 % pairs. Thereby it is important that the key is
18 % followed by the value.
19 % plist - Parameter list with values for the keys
20 % 'params' and 'values'. The values can be a
21 % single value or a cell array with multiple
22 % values.
23 % cell-arrays - Two cell-arrays with the first of the 'params'
24 % and the second with the 'values'.
25 %
26 % REMARK: This function will replace the parameter value if the
27 % parameter name already exist
28 %
29 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'setParameters')">Parameters Description</a>
30 %
31 % VERSION: $Id: setParameters.m,v 1.2 2011/04/28 19:50:57 mauro Exp $
32 %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 function varargout = setParameters(varargin)
36
37 % Check if this is a call from a class method
38 callerIsMethod = utils.helper.callerIsMethod;
39
40 if callerIsMethod
41 sm = varargin{1}; % smodel-object(s)
42 names = varargin{2}; % cell-array with parameter-names
43 values = varargin{3}; % call-array with parameter-values
44
45 else
46 % Check if this is a call for parameters
47 if utils.helper.isinfocall(varargin{:})
48 varargout{1} = getInfo(varargin{3});
49 return
50 end
51
52 import utils.const.*
53 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
54
55 % Collect input variable names
56 in_names = cell(size(varargin));
57 for ii = 1:nargin,in_names{ii} = inputname(ii);end
58
59 % Collect all smodel objects
60 [sm, sm_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
61 [pls, dummy, rest] = utils.helper.collect_objects(rest(:), 'plist');
62
63 names = {};
64 values = {};
65 %%% If the input PLIST have the keys 'params' and 'values' then use also
66 %%% the
67 if length(pls) == 1 && isa(pls, 'plist') && isparam(pls, 'params')
68 names = find(pls, 'params');
69 values = find(pls, 'values');
70 % Make sure that the param names and the values are cell-arrays
71 names = cellstr(names);
72 if ~iscell(values) && ~isempty(values)
73 values = num2cell(values);
74 end
75 end
76
77 % Get the parameter names and the values from the input
78 nRest = numel(rest);
79 if nRest == 1 && iscell(rest{1})
80 % setParameters({'a', 'b'})
81 names = [names rest{1}];
82 elseif ~isempty(rest) && iscellstr(rest)
83 % setParameters('a', 'b', 'c')
84 names = [names rest];
85 elseif nRest == 2 && iscell(rest{1}) && isnumeric(rest{2})
86 % setParameters({'a', 'b'}, [1 2])
87 names = [names rest{1}];
88 values = [values num2cell(rest{2})];
89 elseif nRest == 2 && iscell(rest{1}) && iscell(rest{2})
90 % setParameters({'a', 'b'}, {1 2})
91 names = [names rest{1}];
92 values = [values rest{2}];
93 else
94 % setParameters('a', 1, 'b', 2, 'c', 3)
95 names = [names rest(1:2:nRest)];
96 values = [values rest(2:2:nRest)];
97 end
98
99 % Combine input plists and default PLIST
100 pls = combine(pls, getDefaultPlist());
101
102 end
103
104 % Check that we have the same number of param names as the number of values.
105 if ~isempty(values) && numel(names) ~= numel(values)
106 error('### Please specify for each parameter name [%d] one parameter value [%d]', numel(names), numel(values));
107 end
108
109 % Decide on a deep copy or a modify
110 sm = copy(sm, nargout);
111
112 % Loop over smodel objects
113 for jj = 1:numel(sm)
114
115 % Append the key-value pair
116 sm(jj).params = names;
117 if ~isempty(values)
118 sm(jj).values = values;
119 end
120
121 if ~callerIsMethod
122 plh = pls.pset('params', names);
123 plh.pset('values', values);
124 sm(jj).addHistory(getInfo('None'), plh, sm_invars(jj), sm(jj).hist);
125 end
126 end
127
128 % Set output
129 varargout = utils.helper.setoutputs(nargout, sm);
130 end
131
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 %
134 % FUNCTION: getInfo
135 %
136 % DESCRIPTION: Get Info Object
137 %
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 function ii = getInfo(varargin)
141 if nargin == 1 && strcmpi(varargin{1}, 'None')
142 sets = {};
143 pl = [];
144 else
145 sets = {'Default'};
146 pl = getDefaultPlist();
147 end
148 % Build info object
149 ii = minfo(mfilename, mfilename('class'), 'ltpda', utils.const.categories.helper, '$Id: setParameters.m,v 1.2 2011/04/28 19:50:57 mauro Exp $', sets, pl);
150 end
151
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 %
154 % FUNCTION: getDefaultPlist
155 %
156 % DESCRIPTION: Get Default Plist
157 %
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 function plout = getDefaultPlist()
161 persistent pl;
162 if ~exist('pl', 'var') || isempty(pl)
163 pl = buildplist();
164 end
165 plout = pl;
166 end
167
168 function pl = buildplist()
169 pl = plist();
170
171 % parameter names
172 p = param({'params', 'A cell-array with the parameter names.'}, paramValue.EMPTY_CELL);
173 pl.append(p);
174
175 % parameter values
176 p = param({'values', 'A cell-array with the parameter values.'}, paramValue.EMPTY_CELL);
177 pl.append(p);
178 end
179