-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfold.m
143 lines (134 loc) · 3.87 KB
/
unfold.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function unfold(SC,varargin)
%UNFOLD Unfolds a structure.
% UNFOLD(SC) displays the content of a variable. If SC is a structure it
% recursively shows the name of SC and the fieldnames of SC and their
% contents. If SC is a cell arraythe contents of each cell are displayed.
% It uses the caller's workspace variable name as the name of SC.
% UNFOLD(SC,NAME) uses NAME as the name of SC.
% UNFOLD(SC,SHOW) If SHOW is false only the fieldnames and their sizes
% are shown, if SHOW is true the contents are shown also.
% UNFOLD(SC,NAME,SHOW)
%R.F. Tap
%15-6-2005, 7-12-2005, 5-1-2006, 3-4-2006
% check input
switch nargin
case 1
Name = inputname(1);
show = true;
case 2
if islogical(varargin{1})
Name = inputname(1);
show = varargin{1};
elseif ischar(varargin{1})
Name = varargin{1};
show = true;
else
error('Second input argument must be a string or a logical')
end
case 3
if ischar(varargin{1})
if islogical(varargin{2})
Name = varargin{1};
show = varargin{2};
else
error('Third input argument must be a logical')
end
else
error('Second input argument must be a string')
end
otherwise
error('Invalid number of input arguments')
end
if isstruct(SC)
%number of elements to be displayed
NS = numel(SC);
if show
hmax = NS;
else
hmax = min(1,NS);
end
%recursively display structure including fieldnames
for h=1:hmax
F = fieldnames(SC(h));
NF = length(F);
for i=1:NF
if NS>1
siz = size(SC);
if show
Namei = [Name '(' ind2str(siz,h) ').' F{i}];
else
Namei = [Name '(' ind2str(siz,NS) ').' F{i}];
end
else
Namei = [Name '.' F{i}];
end
if isstruct(SC(h).(F{i}))
unfold(SC(h).(F{i}),Namei,show);
else
if iscell(SC(h).(F{i}))
siz = size(SC(h).(F{i}));
NC = numel(SC(h).(F{i}));
if show
jmax = NC;
else
jmax = 1;
end
for j=1:jmax
if show
Namej = [Namei '{' ind2str(siz,j) '}'];
else
Namej = [Namei '{' ind2str(siz,NC) '}'];
end
disp(Namej)
if show
disp(SC(h).(F{i}){j})
end
end
else
disp(Namei)
if show
disp(SC(h).(F{i}))
end
end
end
end
end
elseif iscell(SC)
%recursively display cell
siz = size(SC);
for i=1:numel(SC)
Namei = [Name '{' ind2str(siz,i) '}'];
unfold(SC{i},Namei,show);
end
else
disp(Name)
if show
disp(SC)
end
end
%local functions
%--------------------------------------------------------------------------
function str = ind2str(siz,ndx)
n = length(siz);
%treat vectors and scalars correctly
if n==2
if siz(1)==1
siz = siz(2);
n = 1;
elseif siz(2)==1
siz = siz(1);
n = 1;
end
end
k = [1 cumprod(siz(1:end-1))];
ndx = ndx - 1;
str = '';
for i = n:-1:1,
v = floor(ndx/k(i))+1;
if i==n
str = num2str(v);
else
str = [num2str(v) ',' str];
end
ndx = rem(ndx,k(i));
end