comparison m-toolbox/classes/+utils/@helper/objdisp.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 % OBJDISP displays the input object.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: OBJDISP displays the input object.
5 %
6 % utils.helper.objdisp(obj)
7 % txt = utils.helper.objdisp(obj) Returns the display text as a cell
8 % array of strings.
9 %
10 % The following call returns an info object for this method.
11 %
12 %
13 % HISTORY: 04-06-08 M Hewitson
14 % Creation
15 %
16 % VERSION: $Id: objdisp.m,v 1.27 2011/04/27 19:35:23 ingo Exp $
17 %
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = objdisp(varargin)
22
23 % Go through each input
24 txt = {};
25 for jj = 1:nargin
26 % get this input
27 objs = varargin{jj};
28
29 % Print emtpy object
30 if isempty(objs)
31 hdr = sprintf('------ %s -------', class(objs));
32 ftr(1:length(hdr)) = '-';
33 txt = [txt; {hdr}];
34 txt = [txt; sprintf('empty-object [%d,%d]',size(objs))];
35 txt = [txt; {ftr}];
36 end
37
38 % go through each object in this input arg
39 for kk = 1:numel(objs)
40 % header
41 obj = objs(kk);
42 hdr = sprintf('------ %s/%d -------', class(obj), kk);
43 ftr(1:length(hdr)) = '-';
44 txt = [txt; {hdr}];
45 % Go through each field of this object
46 fnames = fieldnames(obj);
47 % get longest name for padding
48 maxl = max(cellfun('length', fnames));
49
50 for ii = 1:numel(fnames)
51 f = fnames{ii};
52 fval = obj.(f);
53 padstr = sprintf('%*s', maxl, fnames{ii});
54 npadstr = repmat(' ', 1, length(padstr));
55 % deal with cell-arrays of strings
56 switch class(fval)
57 case 'cell'
58 cs = ['[' num2str(size(fval,1)),'x',num2str(size(fval,2)) ']'];
59 cstr = Cell2String(fval);
60 if isempty(cstr)
61 txt = [txt; [sprintf('%s: ', padstr), '{}']];
62 else
63 str = [sprintf('%s: ', padstr), cstr(1,:)];
64 txt = [txt; str];
65 for mm=2:size(cstr,1)
66 str = [sprintf('%s ', npadstr), cstr(mm,:)];
67 txt = [txt; str];
68 end
69 end
70 txt{end} = [txt{end} ' ' cs];
71 case 'double'
72 MAX_LENGTH = 40;
73 if isempty(fval)
74 str = [sprintf('%s: ', padstr) mat2str(fval)];
75 elseif isvector(fval)
76 str = [sprintf('%s: ', padstr) utils.helper.mat2str(fval(1:min(MAX_LENGTH, numel(fval))))];
77 else
78 str = [sprintf('%s: ', padstr) mat2str(fval(1:min(MAX_LENGTH, numel(fval))))];
79 end
80 if MAX_LENGTH<numel(fval)
81 str = [str(1:end-1) ' ... ]'];
82 end
83 txt = [txt; str];
84 case 'ssmblock'
85 txt = [txt; sprintf('%s: [%dx%d %s]', padstr, size(fval), class(fval))];
86 margin = sprintf('%s: ', padstr);
87 whitespace = '';
88 for mm = 1:(numel(margin)-4)
89 whitespace = [whitespace ' '];
90 end
91 for mm = 1:numel(fval)
92 txt = [txt ; [ sprintf( '%s%s%s%s', whitespace , num2str(mm) ,' : ') strtrim(char(fval(mm))) ] ];
93 end
94 case 'meta.method'
95 if isempty(objs.testMethods)
96 txt = [txt; {sprintf('%s: - No Methods -', padstr)}];
97 else
98 txt = [txt; {sprintf('%s: %s', padstr, fval(1).Name)}];
99 for mm = 2:numel(fval)
100 sp(1:maxl) = ' ';
101 txt = [txt; {sprintf('%s %s', sp, fval(mm).Name)}];
102 end
103 end
104 case 'handle'
105 txt = [txt; sprintf('%s: %s [%dx%d %s]', padstr, strtrim(char(fval)), size(fval), class(fval))];
106 case 'logical'
107 txt = [txt; sprintf('%s: ', padstr) mat2str(fval)];
108 case 'struct'
109 fieldsTxt = utils.helper.objdisp(fval);
110 txt = [txt; sprintf('%s: %s', padstr, fieldsTxt{1})];
111 for qq = 2:numel(fieldsTxt)
112 txt = [txt; sprintf('%s %s', blanks(numel(padstr)), fieldsTxt{qq})];
113 end
114 case 'ltpda_test_runner'
115 txt = [txt; sprintf('%s: %s', padstr, 'ltpda_test_runner')];
116 otherwise
117 txt = [txt; sprintf('%s: %s', padstr, char(fval))];
118 end
119 end
120 txt = [txt; {ftr}];
121 end % end object loop
122 end % end input loop
123
124 if nargout == 0
125 for ii = 1:length(txt)
126 disp(txt{ii});
127 end
128 elseif nargout == 1
129 varargout{1} = txt;
130 end
131 end
132
133 function txt = Cell2String(c)
134 sc = size(c);
135 % recursive code to print the content of cell arrays
136 if iscell(c) % for a cell
137 txt = '';%;
138 for ii = 1:sc(1)
139 if ii == 1
140 txti = '{';
141 else
142 txti = ' ';
143 end
144 for jj = 1:sc(2)
145 txti = [txti ' ' Cell2String(c{ii,jj})];
146 end
147 if ii == size(c,1)
148 txti = [txti, ' }'];
149 end
150 txt = strvcat(txt, txti);
151 end
152 elseif islogical(c) % for a logical
153 txt = mat2str(c);
154 elseif isnumeric(c) || isa(c,'sym') % for a numerical array, only size is displayed
155 if isequal(sc, [0 0])
156 txt = ' [] ';
157 elseif isa(c, 'double') && numel(c)==1
158 txt = ['[' sprintf('%.17g', c) ']'];
159 elseif isa(c,'double') && (norm(c)==0) % for zero array (test dos not carsh for sym)
160 txt = ' [] ';
161 else % for non empty array
162 if sc(1) > 9
163 txt1 = ['[',utils.helper.num2str(sc(1))];
164 else
165 txt1 = [' [',utils.helper.num2str(sc(1))];
166 end
167 if sc(2) > 9
168 txt2 = [utils.helper.num2str(sc(2)),']'];
169 else
170 txt2 = [utils.helper.num2str(sc(2)),'] '];
171 end
172 txt = [txt1,'x',txt2 ];
173 end
174 % txt = mat2str(c); % old display
175 elseif ischar(c)
176 txt = ['''' c ''''];
177 else
178 txt = char(c);
179 end
180 end