Mercurial > hg > ltpda
comparison m-toolbox/classes/@plist/append.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 % APPEND append a param-object, plist-object or a key/value pair to the parameter list. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: APPEND append a param-object, plist-object or a key/value pair to | |
5 % the parameter list. | |
6 % | |
7 % CALL: pl = append(pl, param-object); | |
8 % pl = append(pl, plist-object); | |
9 % pl = append(pl, 'key1', 'value1'); | |
10 % | |
11 % pl = append(pl, combination of the examples above) | |
12 % | |
13 % REMARK: It is not possible to append an key/value pair if the key exist | |
14 % in the parameter list. Tn this case an error is thrown. | |
15 % | |
16 % <a href="matlab:utils.helper.displayMethodInfo('plist', 'append')">Parameters Description</a> | |
17 % | |
18 % VERSION: $Id: append.m,v 1.31 2011/04/08 08:56:21 hewitson Exp $ | |
19 % | |
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
21 | |
22 function varargout = append(varargin) | |
23 | |
24 import utils.const.* | |
25 | |
26 %=== short cuts for simple calls | |
27 | |
28 % pl.append(p) | |
29 if nargout == 0 && nargin == 2 && isa(varargin{2}, 'param') && numel(varargin{2})==1 | |
30 pl = varargin{1}; | |
31 p = varargin{2}; | |
32 if ~isempty(pl.params) && any(strcmpi(p.key, {pl.params.key})) | |
33 error('\n### The key [%s] exist in the parameter list.\n### Please use the function pset.',p.key); | |
34 end | |
35 pl.params = [pl.params p]; | |
36 return | |
37 end | |
38 | |
39 % pl.append('key', value) | |
40 if nargout == 0 && nargin == 3 | |
41 pl = varargin{1}; | |
42 key = varargin{2}; | |
43 if ~isempty(pl.params) && any(strcmpi(key, {pl.params.key})) | |
44 error('\n### The key [%s] exist in the parameter list.\n### Please use the function pset.',key); | |
45 end | |
46 pl.params = [pl.params param(key, varargin{3})]; | |
47 return | |
48 end | |
49 | |
50 %%% Check if this is a call for parameters | |
51 if utils.helper.isinfocall(varargin{:}) | |
52 varargout{1} = getInfo(varargin{3}); | |
53 return | |
54 end | |
55 | |
56 | |
57 [objs, invars, rest] = utils.helper.collect_objects(varargin(:), 'plist'); | |
58 [pps, invars, rest] = utils.helper.collect_objects(rest(:), 'param'); | |
59 | |
60 %%% Decide on a deep copy or a modify | |
61 pls = copy(objs, nargout); | |
62 | |
63 %%% REMARK: If the rest is an single string and the number of plist is two | |
64 %%% then we assume that the rest and the second plist are a key/value | |
65 %%% pair. | |
66 if numel(rest) == 1 && ischar(rest{1}) && numel(objs) == 2 | |
67 rest{2} = objs(2); | |
68 pls(2) = []; | |
69 end | |
70 | |
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
72 %%%%%%%%%% First Case: Append plist-objects %%%%%%%%%% | |
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
74 | |
75 pl = pls(1); | |
76 | |
77 %%% If we found more than one plist then append the parameters | |
78 %%% of the second, third, ... plist to the first plist | |
79 if numel (pls) > 1 | |
80 for kk = 2:numel(pls) | |
81 for jj = 1:length(pls(kk).params) | |
82 add_param(pl, pls(kk).params(jj)); | |
83 end | |
84 end | |
85 end | |
86 | |
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
88 %%%%%%%%%% Second Case: Append param-objects %%%%%%%%%% | |
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
90 | |
91 if ~isempty(pps) | |
92 for kk = 1:numel(pps) | |
93 add_param(pl, pps(kk)); | |
94 end | |
95 end | |
96 | |
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
98 %%%%%%%%%% Third Case: Append key/value pairs %%%%%%%%%% | |
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
100 | |
101 if ~isempty(rest) | |
102 if mod(numel(rest),2) | |
103 error('### Please define a ''key'' AND a ''value''%s### to append this pair.', char(10)); | |
104 end | |
105 while ~isempty(rest) | |
106 if iscell(rest{1}) | |
107 key = rest{1}{1}; | |
108 desc = rest{1}{2}; | |
109 else | |
110 desc = ''; | |
111 key = rest{1}; | |
112 end | |
113 val = rest{2}; | |
114 | |
115 %%% Remove the first two objects from the 'rest' variable | |
116 rest(1) = []; | |
117 rest(1) = []; | |
118 | |
119 % does the key exist? | |
120 if isempty(pl.params) | |
121 idx = []; | |
122 else | |
123 idx = find(strcmpi(key, {pl.params.key})); | |
124 end | |
125 | |
126 if isempty(idx) | |
127 % add a new param | |
128 if isempty(desc) | |
129 pl.params = [pl.params param(key,val)]; | |
130 else | |
131 if ~ischar(desc) | |
132 error('### The description for a parameter must be a string but it is from the class [%s]', class(desc)); | |
133 else | |
134 pl.params = [pl.params param({key, desc},val)]; | |
135 end | |
136 end | |
137 else | |
138 error('\n### The key [%s] exist in the parameter list.\n### Please use the function pset.',key); | |
139 end | |
140 | |
141 end | |
142 end | |
143 | |
144 varargout{1} = pl; | |
145 end | |
146 | |
147 | |
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
149 % Local Functions % | |
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
151 | |
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
153 % DESCRIPTION: The input parameter will only be added if the key doesn't exist | |
154 % in the plist. Throw an error if the key exist in the plist. | |
155 function add_param(pl, pp) | |
156 | |
157 if ~isempty(pl.params) && any(strcmpi({pl.params.key}, pp.key)) | |
158 error('\n### The key [%s] exist in the parameter list.\n### Please use the function pset.', pp.key); | |
159 return | |
160 end | |
161 pl.params = [pl.params pp]; | |
162 end | |
163 | |
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
165 % | |
166 % FUNCTION: getInfo | |
167 % | |
168 % DESCRIPTION: Get Info Object | |
169 % | |
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
171 | |
172 function ii = getInfo(varargin) | |
173 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
174 sets = {}; | |
175 pl = []; | |
176 else | |
177 sets = {'Default'}; | |
178 pl = getDefaultPlist; | |
179 end | |
180 % Build info object | |
181 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.helper, '$Id: append.m,v 1.31 2011/04/08 08:56:21 hewitson Exp $', sets, pl); | |
182 ii.setArgsmin(1); | |
183 end | |
184 | |
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
186 % | |
187 % FUNCTION: getDefaultPlist | |
188 % | |
189 % DESCRIPTION: Get Default Plist | |
190 % | |
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
192 | |
193 function plo = getDefaultPlist() | |
194 plo = plist(); | |
195 end | |
196 |