-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShowCellTree.m
111 lines (84 loc) · 2.56 KB
/
ShowCellTree.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function ShowCellTree(C)
% SHOWCELLTREE display the content of cells recursively, such as celldisp
% (form matlab) but with a "tree" look.
%
% See also celldisp (official MATLAB function) ShowStructTree (created by a
% user and published on Mathworks)
%% Paramters
if nargin<1
error('Not enough input arguments.')
end
%% Initialize global variables
global cell_level % a counter for the levels (branches of the tree)
% Reset the variable only if the caller of the function is not herself
stacks = dbstack;
if length(stacks)>1
if ~strcmp(stacks(2).name,mfilename)
cell_level = 0;
end
else
cell_level = 0;
end
%% Recursive print
if iscell(C) && ~isempty(C)
% Here I use linear indexing so the function can deal whith unknown
% multidimentional cells.
Size = size(C);
N = numel(C);
% Pretty display of the "trunk", the input cell
if cell_level == 0
fprintf('%s%s \n',inputname(1),subs(N,Size))
end
% Time to go deeper ?
cell_level = cell_level + 1;
for i = 1:N
fprintf('\n %s%s%s',char(repmat([124 9],[1 cell_level-1])),char([124 45 45 45]),subs(i,Size)) % Print the current element of the cell
ShowCellTree(C{i}) % Go deeper : gi inside the current element of the cell (here is the recursivity)
end
cell_level = cell_level - 1;
else
% Display the content of the cell (non-empty)
if ~isempty(C)
if ischar(C)
for i = 1:size(C,1)
fprintf(' = %s',C(i,:))
end
elseif isnumeric(C)
fprintf(' = %g',C)
else
[m,n] = size(C);
fprintf(' = %0.f-by-%0.f %s',m,n,class(C))
end
else % Display the content of the cell (empty)
if iscell(C)
fprintf(' = {}')
elseif ischar(C)
fprintf(' = ''''')
elseif isnumeric(C)
fprintf(' = []')
else
[m,n] = size(C);
fprintf(' = %0.f-by-%0.f %s',m,n,class(C))
end
end
end
if cell_level == 0 % Only at the first stack of this recursive function.
fprintf('\n')
end
end % function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This sub-function was taken from 'celldisp.m' from MATLAB R2011b
function s = subs(i,siz)
%SUBS Display subscripts
if length(siz)==2 && any(any(siz==1))
v = cell(1,1);
else
v = cell(size(siz));
end
[v{1:end}] = ind2sub(siz,i);
s = ['{' int2str(v{1})];
for i=2:length(v)
s = [s ',' int2str(v{i})]; %#ok<AGROW>
end
s = [s '}'];
end % function