comparison m-toolbox/classes/@ltpda_uoh/index.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 % INDEX index into a 'ltpda_uoh' object array or matrix. This properly captures the history.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: INDEX into an 'ltpda_uoh' object array or matrix.
5 % This properly captures the history.
6 %
7 % CALL: b = index(a, i)
8 % b = index(a, i, j)
9 % b = a.index(plist('I', i))
10 % b = a.index(plist('I', i, 'J', j))
11 %
12 % <a href="matlab:utils.helper.displayMethodInfo('ltpda_uoh', 'index')">Parameters Description</a>
13 %
14 % VERSION: $Id: index.m,v 1.13 2011/04/08 08:56:30 hewitson Exp $
15 %
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18
19 function varargout = index(varargin)
20
21 % Check if this is a call for parameters
22 if utils.helper.isinfocall(varargin{:})
23 varargout{1} = getInfo(varargin{3});
24 return
25 end
26
27 if nargout == 0
28 error('### index cannot be used as a modifier. Please give an output variable.');
29 end
30
31 import utils.const.*
32 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
33
34 % We can only effectively handle a single matrix of 'ltpda_uoh' objects
35 af = 0;
36 for j=1:nargin
37 if isa(varargin{j}, 'ltpda_uoh')
38 af = af + 1;
39 end
40 end
41 if af > 1
42 error('### Input a single matrix of ''ltpda_uoh'' objects for indexing.');
43 end
44
45 % Collect input variable names
46 in_names = cell(size(varargin));
47 for ii = 1:nargin,in_names{ii} = inputname(ii);end
48
49 % Collect all 'ltpda_uoh' objects and plists
50 [aobjs, obj_invars] = utils.helper.collect_objects(varargin(:), 'ltpda_uoh', in_names);
51 [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
52
53 % Decide on a deep copy or a modify
54 bobjs = copy(aobjs, nargout);
55
56 % Combine plists
57 pl = combine(pl, getDefaultPlist);
58
59 % Get indices
60 idxi = find(pl, 'i');
61 idxj = find(pl, 'j');
62 if isempty(idxi) || isempty(idxj)
63 % go through the other inputs
64 for j=1:numel(rest)
65 if isnumeric(rest{j}) && isempty(idxi)
66 idxi = rest{j};
67 elseif isnumeric(rest{j}) && isempty(idxj)
68 idxj = rest{j};
69 end
70 end
71 end
72
73 if isempty(idxi) && isempty(idxj)
74 error('### Please provide an index of index pair.');
75 end
76
77 % Gather the input histories
78 inhists = [];
79 for j=1:numel(bobjs)
80 inhists = [inhists bobjs(j).hist];
81 end
82
83 % Now index with either (i) or (i,j)
84 if isempty(idxj)
85 aout = bobjs(idxi);
86 inhists = inhists(idxi);
87 else
88 aout = bobjs(idxi, idxj);
89 inhists = inhists(sub2ind(size(bobjs), idxi, idxj));
90 end
91
92 % Make sure we store the indices
93 pl = pl.pset('i', idxi);
94 if ~isempty(idxj)
95 pl = pl.pset('j', idxj);
96 end
97
98 % Name and add history to all outputs
99 for j=1:numel(aout)
100 if isempty(idxj)
101 % aout(j).name = sprintf('(%s)(%d)', obj_invars{idxi(j)}, idxi(j));
102 aout(j).addHistory(getInfo('None'), pl, obj_invars(idxi), inhists(j));
103 else
104 % aout(j).name = sprintf('%s', obj_invars{sub2ind(size(aobjs), idxi,idxj)});
105 aout(j).addHistory(getInfo('None'), pl, obj_invars(sub2ind(size(aobjs), idxi,idxj)), inhists(j));
106 end
107 end
108
109 % Set output
110 varargout{1} = aout;
111 end
112
113 %--------------------------------------------------------------------------
114 % Get Info Object
115 %--------------------------------------------------------------------------
116 function ii = getInfo(varargin)
117 if nargin == 1 && strcmpi(varargin{1}, 'None')
118 sets = {};
119 pl = [];
120 else
121 sets = {'Default'};
122 pl = getDefaultPlist;
123 end
124 % Build info object
125 ii = minfo(mfilename, 'ltpda_uoh', 'ltpda', utils.const.categories.helper, '$Id: index.m,v 1.13 2011/04/08 08:56:30 hewitson Exp $', sets, pl);
126 ii.setModifier(false);
127 end
128
129 %--------------------------------------------------------------------------
130 % Get Default Plist
131 %--------------------------------------------------------------------------
132 function plout = getDefaultPlist()
133 persistent pl;
134 if exist('pl', 'var')==0 || isempty(pl)
135 pl = buildplist();
136 end
137 plout = pl;
138 end
139
140 function pl = buildplist()
141 pl = plist('I', [], 'J', []);
142 end
143