Mercurial > hg > ltpda
comparison m-toolbox/classes/@plist/merge.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 % MERGE the values for the same key of multiple parameter lists together. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: MERGE the values for the same key of multiple parameter | |
5 % lists together. | |
6 % | |
7 % CALL: pl = merge(p1, p2, p3); | |
8 % pl = merge(p1, [p2 p3], p4) | |
9 % | |
10 % EXAMPLES: >> pl1 = plist('A', 1); | |
11 % >> pl2 = plist('A', 3); | |
12 % >> plo = merge(pl1, pl2) | |
13 % | |
14 % Then plo will contain a parameter 'A' with value [1 3]. | |
15 % | |
16 % <a href="matlab:utils.helper.displayMethodInfo('plist', 'merge')">Parameters Description</a> | |
17 % | |
18 % VERSION: $Id: merge.m,v 1.5 2011/04/08 08:56:21 hewitson Exp $ | |
19 % | |
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
21 | |
22 function varargout = merge(varargin) | |
23 | |
24 %%% Check if this is a call for parameters | |
25 if utils.helper.isinfocall(varargin{:}) | |
26 varargout{1} = getInfo(varargin{3}); | |
27 return | |
28 end | |
29 | |
30 if nargout == 0 | |
31 error('### This method cannot be used as a modifier. Please give an output variable.'); | |
32 end | |
33 | |
34 dpl = getDefaultPlist(); | |
35 concatenateStrings = dpl.find('concatenate strings'); | |
36 removeDuplicates = dpl.find('remove duplicates'); | |
37 | |
38 % Collect all PLIST. | |
39 objs = []; | |
40 for ii = 1:nargin | |
41 if isa(varargin{ii}, 'plist') | |
42 if numel(varargin{ii}) == 1 && ... | |
43 ((varargin{ii}.nparams == 1 && ... | |
44 (varargin{ii}.isparam('remove duplicates') || ... | |
45 varargin{ii}.isparam('concatenate strings'))) || ... | |
46 (varargin{ii}.nparams == 2 && ... | |
47 varargin{ii}.isparam('remove duplicates') && ... | |
48 varargin{ii}.isparam('concatenate strings'))) | |
49 % For example: | |
50 % plist('remove duplicates', true) | |
51 % plist('concatenate strings', ', ') | |
52 % plist('remove duplicates', true, 'concatenate strings', ', ') | |
53 removeDuplicates = varargin{ii}.find('remove duplicates'); | |
54 concatenateStrings = varargin{ii}.find('concatenate strings'); | |
55 else | |
56 if isempty(objs) | |
57 objs = varargin{ii}; | |
58 else | |
59 objs = [reshape(objs, 1, []), reshape(varargin{ii}, 1, [])]; | |
60 end | |
61 end | |
62 end | |
63 end | |
64 | |
65 keys = {}; | |
66 values = struct(); | |
67 | |
68 for ii = 1:numel(objs) | |
69 % Loop over all params in the current plist | |
70 for jj = 1:length(objs(ii).params) | |
71 | |
72 key = objs(ii).params(jj).key; | |
73 val = objs(ii).params(jj).val.getVal(); | |
74 | |
75 idxKey = strmatch(key, keys); | |
76 | |
77 if isempty(idxKey) | |
78 keys = [keys; {key}]; | |
79 values.(sprintf('KEY%d', length(keys))) = {val}; | |
80 else | |
81 name = sprintf('KEY%d', idxKey); | |
82 values.(name) = [values.(name) {val}]; | |
83 end | |
84 | |
85 end | |
86 end | |
87 | |
88 % Build new PLIST | |
89 pl = plist(); | |
90 | |
91 for ii = 1:numel(keys) | |
92 key = keys{ii}; | |
93 name = sprintf('KEY%d', ii); | |
94 val = values.(name); | |
95 | |
96 if iscellstr(val) && ~isempty(val) | |
97 | |
98 % Keep cell or concatenate the strings? | |
99 if isempty(concatenateStrings) | |
100 % Keep cell | |
101 s = val; | |
102 else | |
103 s = val{1}; | |
104 for ss = 2:numel(val) | |
105 s = [s, concatenateStrings, val{ss}]; | |
106 end | |
107 end | |
108 p = param(key, s); | |
109 | |
110 else | |
111 | |
112 % Get class of each value | |
113 classType = unique(cellfun(@class, val, 'UniformOutput', false)); | |
114 | |
115 if numel(classType) == 1 | |
116 % Here we are sure that the values have the same type. | |
117 p = param(key, [val{:}]); | |
118 else | |
119 % Here we are sure that the values does NOT have the same type. | |
120 p = param(key, val); | |
121 end | |
122 end | |
123 | |
124 pl.append(p); | |
125 | |
126 end | |
127 | |
128 varargout{1} = pl; | |
129 end | |
130 | |
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
132 % Local Functions % | |
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
134 | |
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
136 % | |
137 % FUNCTION: getInfo | |
138 % | |
139 % DESCRIPTION: Get Info Object | |
140 % | |
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
142 | |
143 function ii = getInfo(varargin) | |
144 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
145 sets = {}; | |
146 pl = []; | |
147 else | |
148 sets = {'Default'}; | |
149 pl = getDefaultPlist; | |
150 end | |
151 % Build info object | |
152 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.helper, '$Id: merge.m,v 1.5 2011/04/08 08:56:21 hewitson Exp $', sets, pl); | |
153 ii.setArgsmin(2); | |
154 ii.setModifier(false); | |
155 end | |
156 | |
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
158 % | |
159 % FUNCTION: getDefaultPlist | |
160 % | |
161 % DESCRIPTION: Get Default Plist | |
162 % | |
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
164 | |
165 function plo = getDefaultPlist() | |
166 plo = plist(); | |
167 | |
168 % p = param({'remove duplicates', 'Don''t keep duplicates for the same key.'}, paramValue.TRUE_FALSE); | |
169 % plo.append(p); | |
170 | |
171 p = param({'concatenate strings', 'Specify a separator for concatenating strings.'}, paramValue.EMPTY_STRING); | |
172 plo.append(p); | |
173 | |
174 end | |
175 |