comparison m-toolbox/classes/+utils/@helper/collect_objects.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 % COLLECT_OBJECTS Collect objects of the required class.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: COLLECT_OBJECTS Collect objects of the required class.
5 %
6 % CALL: [objs, invars, rest] = collect_objects(varargin, class_type, in_names);
7 % [objs, invars] = collect_objects(varargin, class_type, in_names);
8 % [objs] = collect_objects(varargin, class_type, in_names);
9 % [objs] = collect_objects(varargin, class_type);
10 % [objs] = collect_objects(varargin, '');
11 %
12 % INPUTS: varargin: Cell array of objects
13 % class_name: Class name of the collected objects
14 % If the class_name is empty then this function
15 % collects all objects of the same class as
16 % the first ltpda_object.
17 % in_names: Cell array of corresponding variable names of
18 % the contents in varargin
19 %
20 % OUTPUTS: objs: Collection of all required class objects.
21 % invars: Collection of the object names of the corresponding object.
22 % rest: Rest of all other objects collected in a cell array.
23 %
24 % VERSION: $Id: collect_objects.m,v 1.15 2011/04/19 16:24:36 ingo Exp $
25 %
26 % HISTORY: 08-05-07 Diepholz
27 % Creation
28 %
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30
31 function varargout = collect_objects(varargin, class_type, in_names)
32
33 %%% collect input objects and corresponding variable names
34
35 %%% If the class_type is empty then collect all objects of the first ltpda object
36 if isempty(class_type)
37 for ii = 1:length(varargin)
38 if isa(varargin{ii}, 'ltpda_obj')
39 class_type = class(varargin{ii});
40 break;
41 end
42 end
43 end
44
45 % Count the number of arguments that contain this class type
46 Narg = length(varargin);
47 classmatch = zeros(Narg,1);
48 Nclass = 0;
49 for ii=1:Narg
50 if isa(varargin{ii}, class_type)
51 classmatch(ii) = 1;
52 Nclass = Nclass + 1;
53 end
54 end
55
56 invars = {};
57 objs = [];
58 other = {};
59
60 for ii=1:Narg
61 vii = varargin{ii};
62 if classmatch(ii)
63 if Nclass == 1
64 objs = vii;
65 else
66 if numel(vii) == 1
67 objs = [objs vii];
68 else
69 objs = [objs reshape(vii, 1, [])];
70 end
71 end
72
73 %%% Define the variable name only if the input names are specified.
74 if nargin == 3 && nargout > 1
75
76 % It is possible for a internal-call that in_names is empty
77 % Create forthis an empty cell-array wit the size of varargin
78 if isempty(in_names)
79 in_names = cell(size(varargin));
80 end
81
82 def_name = in_names{ii};
83 if isempty(def_name)
84 def_name = 'unknown';
85 end
86
87 % Memorise the variable name of the corresponding object.
88 % If the object is an array or vector add the index to the variable name
89 %
90 % we choose the object name if it is not set to the default
91 % and it's not empty.
92 if (numel(vii) == 1) || (numel(vii) == 0)
93 if classmatch(ii)
94 try
95 if ~strncmpi(vii.name, 'None', 4)
96 if ~isempty(vii.name)
97 def_name = vii.name;
98 end
99 end
100 end
101 end
102 invars{end+1} = def_name;
103 elseif classmatch(ii)
104 if isa(vii, 'ltpda_uo')
105 % Check the elements have all the same names
106 sameNames = true;
107 name = vii(1).name;
108 for jj=2:numel(vii)
109 if ~strcmp(name, vii(jj).name)
110 sameNames = false;
111 break;
112 end
113 end
114
115 if sameNames
116 if ~strcmpi(vii(1).name, 'None') && ~isempty(vii(1).name)
117 def_name = vii(1).name;
118 end
119 Nargii = size(vii,1);
120 for jj=1:numel(vii)
121 invars{end+1} = mind2subStr(def_name, Nargii,jj);
122 end
123 else
124 for jj=1:numel(vii)
125 invars{end+1} = vii(jj).name;
126 end
127 end
128 else
129 % The objects don't have a 'name'
130 Nargii = size(vii,1);
131 for jj=1:numel(vii)
132 invars{end+1} = mind2subStr(def_name, Nargii,jj);
133 end
134 end
135 else
136 invars{end+1} = def_name;
137 end
138 end
139
140 else
141 if nargout > 2
142 other{end+1} = vii;
143 end
144 end
145
146 end
147
148 % Collect outputs
149 if nargout >= 1
150 varargout{1} = objs;
151 end
152 if nargout >= 2
153 varargout{2} = invars;
154 end
155 if nargout >= 3
156 varargout{3} = other;
157 end
158 end
159
160 %--------------------------------------------------------
161 % A much simplified special case of ind2sub to collect just
162 % the names.
163 %
164 function s = mind2subStr(name, siz, ndx)
165
166 % Get J
167 vi = rem(ndx-1, siz) + 1;
168 J = (ndx-vi)/siz + 1;
169
170 % Get I
171 ndx = vi;
172 vi = rem(ndx-1, 1) + 1;
173 s = sprintf('%s(%d,%d)', name, (ndx - vi) + 1,J);
174 end