comparison m-toolbox/classes/@plist/pset.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 % PSET set or add a key/value pairor a param-object into the parameter list.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: PSET set or add a key/value pairor a param-object
5 % into the parameter list. Exist the key in the parameter list
6 % then becomes the value the new value.
7 %
8 % CALL: pl = pset(pl, param-object);
9 % pl = pset(pl, key, val);
10 % pl = pset(pl, key1, val1, key2, val2);
11 %
12 % REMARK: For the input objects exist the rule that the key must always be
13 % followed by the value.
14 %
15 % <a href="matlab:utils.helper.displayMethodInfo('plist', 'pset')">Parameters Description</a>
16 %
17 % VERSION: $Id: pset.m,v 1.25 2011/04/08 08:56:20 hewitson Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = pset(varargin)
22
23 %%% Check if this is a call for parameters
24 if utils.helper.isinfocall(varargin{:})
25 varargout{1} = getInfo(varargin{3});
26 return
27 end
28
29 objs = [];
30 pps = [];
31 rest = {};
32
33 argin = varargin;
34
35 while numel(argin)
36
37 if isa(argin{1}, 'plist')
38 if isempty(objs)
39 objs = argin{1};
40 else
41 objs = [reshape(objs, 1, []), reshape(argin{1}, 1, [])];
42 end
43 argin(1) = [];
44 elseif isa(argin{1}, 'param')
45 pps = [reshape(pps, 1, []), reshape(argin{1}, 1, [])];
46 argin(1) = [];
47 else
48 if numel(argin) < 2
49 error('### Please define a ''value'' for the ''key'' [%s].', argin{1});
50 end
51 rest{end+1} = argin{1};
52 rest{end+1} = argin{2};
53 argin(1) = [];
54 argin(1) = [];
55 end
56 end
57
58 %%% Decide on a deep copy or a modify
59 if nargout == 0
60 pls = objs;
61 else
62 pls = copy(objs, 1);
63 end
64
65 %%%%%%%%%% Some plausibility checks %%%%%%%%%%
66 if (isempty(pps) && isempty(rest)) || mod(numel(rest),2)
67 error('### Please define a ''key'' AND a ''value''%s### to set this pair.', char(10));
68 end
69
70 for ii = 1:numel(pls)
71
72 pl = pls(ii);
73
74 %%%%%%%%%% First case: Set param-objects %%%%%%%%%%
75 if ~isempty(pps)
76 for jj = 1:numel(pps)
77 add_param(pl, pps(jj));
78 end
79 end
80
81 %%%%%%%%%% Second case: Set key/value pair %%%%%%%%%%
82 rest_help = rest;
83 while ~isempty(rest_help)
84 key = rest_help{1};
85 val = rest_help{2};
86
87 %%% Remove the first two objects from the 'rest_help' variable
88 rest_help(1) = [];
89 rest_help(1) = [];
90
91 % does the key exist?
92 if isempty(pl.params)
93 idx = [];
94 else
95 idx = find(strcmpi(key, {pl.params.key}));
96 end
97
98 if isempty(idx)
99 % add a new param
100 pl.params = [pl.params param(key,val)];
101 else
102 % set existing param value
103 pl.params(idx).setVal(val);
104 end
105
106 end
107 end
108
109 % Set output
110 varargout{1} = pls;
111 end
112
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % Local Functions %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % DESCRIPTION: check to see if this is one of the parameters we can set,
119 % otherwise add it.
120 function add_param(pl, pp)
121 found = 0;
122 for j=1:length(pl.params)
123 if strcmpi(pl.params(j).key, pp.key)
124 pl.params(j).setVal(pp.getVal);
125 found = 1;
126 break
127 end
128 end
129 % add this parameter if necessary
130 if ~found
131 % To be sure that the param object is a copy
132 pp = copy(pp, 1);
133 pl.params = [pl.params pp];
134 end
135 end
136
137
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %
140 % FUNCTION: getInfo
141 %
142 % DESCRIPTION: Get Info Object
143 %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 function ii = getInfo(varargin)
147 if nargin == 1 && strcmpi(varargin{1}, 'None')
148 sets = {};
149 pl = [];
150 else
151 sets = {'Default'};
152 pl = getDefaultPlist;
153 end
154 % Build info object
155 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.helper, '$Id: pset.m,v 1.25 2011/04/08 08:56:20 hewitson Exp $', sets, pl);
156 ii.setArgsmin(1);
157 end
158
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 %
161 % FUNCTION: getDefaultPlist
162 %
163 % DESCRIPTION: Get Default Plist
164 %
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166
167 function plo = getDefaultPlist()
168 plo = plist();
169 end
170