|
1 | 1 | function unfold(input, varargin) |
2 | | - % |
3 | | - % Unfolds a structure. |
4 | | - % |
5 | | - % USAGE:: |
6 | | - % |
7 | | - % unfold(SC, varargin) |
8 | | - % |
9 | | - % UNFOLD(SC) displays the content of a variable. |
10 | | - % |
11 | | - % If SC is a structure it recursively shows the name of SC and the fieldnames of SC and their |
12 | | - % contents. |
13 | | - % |
14 | | - % If SC is a cell array the contents of each cell are displayed. |
15 | | - % |
16 | | - % It uses the caller's workspace variable name as the name of SC. |
17 | | - % UNFOLD(SC,NAME) uses NAME as the name of SC. |
18 | | - % |
19 | | - % UNFOLD(SC,SHOW) If SHOW is false only the fieldnames and their sizes |
20 | | - % are shown, if SHOW is true the contents are shown also. |
21 | | - % |
22 | | - % (C) Copyright 2022 Remi Gau |
23 | | - % (C) Copyright 2005-2006 R.F. Tap |
24 | | - |
25 | | - % R.F. Tap |
26 | | - % 15-6-2005, 7-12-2005, 5-1-2006, 3-4-2006 |
27 | | - |
28 | | - % taken and adapted from shorturl.at/dqwN7 |
29 | | - |
30 | | - % check input |
31 | | - switch nargin |
32 | | - case 1 |
33 | | - Name = inputname(1); |
34 | | - show = true; |
35 | | - case 2 |
36 | | - if islogical(varargin{1}) |
37 | | - Name = inputname(1); |
38 | | - show = varargin{1}; |
39 | | - elseif ischar(varargin{1}) |
40 | | - Name = varargin{1}; |
41 | | - show = true; |
42 | | - else |
43 | | - error('Second input argument must be a string or a logical'); |
44 | | - end |
45 | | - case 3 |
46 | | - if ischar(varargin{1}) |
47 | | - if islogical(varargin{2}) |
48 | | - Name = varargin{1}; |
49 | | - show = varargin{2}; |
50 | | - else |
51 | | - error('Third input argument must be a logical'); |
52 | | - end |
53 | | - else |
54 | | - error('Second input argument must be a string'); |
55 | | - end |
56 | | - otherwise |
57 | | - error('Invalid number of input arguments'); |
58 | | - end |
59 | | - |
60 | | - if isstruct(input) |
61 | | - |
62 | | - input = orderfields(input); |
63 | | - % number of elements to be displayed |
64 | | - NS = numel(input); |
65 | | - if show |
66 | | - hmax = NS; |
67 | | - else |
68 | | - hmax = min(1, NS); |
| 2 | + % |
| 3 | + % Unfolds a structure. |
| 4 | + % |
| 5 | + % USAGE:: |
| 6 | + % |
| 7 | + % unfold(SC, varargin) |
| 8 | + % |
| 9 | + % UNFOLD(SC) displays the content of a variable. |
| 10 | + % |
| 11 | + % If SC is a structure it recursively shows the name of SC and the fieldnames of SC and their |
| 12 | + % contents. |
| 13 | + % |
| 14 | + % If SC is a cell array the contents of each cell are displayed. |
| 15 | + % |
| 16 | + % It uses the caller's workspace variable name as the name of SC. |
| 17 | + % UNFOLD(SC,NAME) uses NAME as the name of SC. |
| 18 | + % |
| 19 | + % UNFOLD(SC,SHOW) If SHOW is false only the fieldnames and their sizes |
| 20 | + % are shown, if SHOW is true the contents are shown also. |
| 21 | + % |
| 22 | + % (C) Copyright 2022 Remi Gau |
| 23 | + % (C) Copyright 2005-2006 R.F. Tap |
| 24 | + |
| 25 | + % R.F. Tap |
| 26 | + % 15-6-2005, 7-12-2005, 5-1-2006, 3-4-2006 |
| 27 | + |
| 28 | + % taken and adapted from shorturl.at/dqwN7 |
| 29 | + |
| 30 | + % check input |
| 31 | + switch nargin |
| 32 | + case 1 |
| 33 | + Name = inputname(1); |
| 34 | + show = true; |
| 35 | + case 2 |
| 36 | + if islogical(varargin{1}) |
| 37 | + Name = inputname(1); |
| 38 | + show = varargin{1}; |
| 39 | + elseif ischar(varargin{1}) |
| 40 | + Name = varargin{1}; |
| 41 | + show = true; |
| 42 | + else |
| 43 | + error('Second input argument must be a string or a logical'); |
| 44 | + end |
| 45 | + case 3 |
| 46 | + if ischar(varargin{1}) |
| 47 | + if islogical(varargin{2}) |
| 48 | + Name = varargin{1}; |
| 49 | + show = varargin{2}; |
| 50 | + else |
| 51 | + error('Third input argument must be a logical'); |
| 52 | + end |
| 53 | + else |
| 54 | + error('Second input argument must be a string'); |
| 55 | + end |
| 56 | + otherwise |
| 57 | + error('Invalid number of input arguments'); |
69 | 58 | end |
70 | 59 |
|
71 | | - % recursively display structure including fieldnames |
72 | | - for h = 1:hmax |
73 | | - F = fieldnames(input(h)); |
74 | | - NF = length(F); |
75 | | - for i = 1:NF |
76 | | - |
77 | | - if NS > 1 |
78 | | - siz = size(input); |
79 | | - if show |
80 | | - Namei = [Name '(' indToStr(siz, h) ').' F{i}]; |
81 | | - else |
82 | | - Namei = [Name '(' indToStr(siz, NS) ').' F{i}]; |
83 | | - end |
84 | | - else |
85 | | - Namei = [Name '.' F{i}]; |
86 | | - end |
| 60 | + if isstruct(input) |
87 | 61 |
|
88 | | - if isstruct(input(h).(F{i})) |
89 | | - unfold(input(h).(F{i}), Namei, show); |
| 62 | + input = orderfields(input); |
| 63 | + % number of elements to be displayed |
| 64 | + NS = numel(input); |
| 65 | + if show |
| 66 | + hmax = NS; |
90 | 67 | else |
| 68 | + hmax = min(1, NS); |
| 69 | + end |
91 | 70 |
|
92 | | - if iscell(input(h).(F{i})) |
93 | | - if numel(input(h).(F{i})) == 0 |
94 | | - printKeyToScreen(Namei); |
95 | | - fprintf(' =\t{};'); |
96 | | - else |
97 | | - siz = size(input(h).(F{i})); |
98 | | - NC = numel(input(h).(F{i})); |
99 | | - if show |
100 | | - jmax = NC; |
101 | | - else |
102 | | - jmax = 1; |
103 | | - end |
104 | | - for j = 1:jmax |
105 | | - if show |
106 | | - Namej = [Namei '{' indToStr(siz, j) '}']; |
| 71 | + % recursively display structure including fieldnames |
| 72 | + for h = 1:hmax |
| 73 | + F = fieldnames(input(h)); |
| 74 | + NF = length(F); |
| 75 | + for i = 1:NF |
| 76 | + |
| 77 | + if NS > 1 |
| 78 | + siz = size(input); |
| 79 | + if show |
| 80 | + Namei = [Name '(' indToStr(siz, h) ').' F{i}]; |
| 81 | + else |
| 82 | + Namei = [Name '(' indToStr(siz, NS) ').' F{i}]; |
| 83 | + end |
107 | 84 | else |
108 | | - Namej = [Namei '{' indToStr(siz, NC) '}']; |
| 85 | + Namei = [Name '.' F{i}]; |
109 | 86 | end |
110 | | - printKeyToScreen(Namej); |
111 | | - if show |
112 | | - printValueToScreen(input(h).(F{i}){j}); |
| 87 | + |
| 88 | + if isstruct(input(h).(F{i})) |
| 89 | + unfold(input(h).(F{i}), Namei, show); |
| 90 | + else |
| 91 | + |
| 92 | + if iscell(input(h).(F{i})) |
| 93 | + if numel(input(h).(F{i})) == 0 |
| 94 | + printKeyToScreen(Namei); |
| 95 | + fprintf(' =\t{};'); |
| 96 | + else |
| 97 | + siz = size(input(h).(F{i})); |
| 98 | + NC = numel(input(h).(F{i})); |
| 99 | + if show |
| 100 | + jmax = NC; |
| 101 | + else |
| 102 | + jmax = 1; |
| 103 | + end |
| 104 | + for j = 1:jmax |
| 105 | + if show |
| 106 | + Namej = [Namei '{' indToStr(siz, j) '}']; |
| 107 | + else |
| 108 | + Namej = [Namei '{' indToStr(siz, NC) '}']; |
| 109 | + end |
| 110 | + printKeyToScreen(Namej); |
| 111 | + if show |
| 112 | + printValueToScreen(input(h).(F{i}){j}); |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + else |
| 118 | + printKeyToScreen(Namei); |
| 119 | + if show |
| 120 | + printValueToScreen(input(h).(F{i})); |
| 121 | + end |
| 122 | + end |
| 123 | + |
113 | 124 | end |
114 | | - end |
115 | 125 | end |
| 126 | + end |
116 | 127 |
|
117 | | - else |
118 | | - printKeyToScreen(Namei); |
119 | | - if show |
120 | | - printValueToScreen(input(h).(F{i})); |
121 | | - end |
122 | | - end |
| 128 | + elseif iscell(input) |
123 | 129 |
|
| 130 | + if numel(input) == 0 |
| 131 | + fprintf(' =\t{};'); |
| 132 | + else |
| 133 | + % recursively display cell |
| 134 | + siz = size(input); |
| 135 | + for i = 1:numel(input) |
| 136 | + Namei = [Name '{' indToStr(siz, i) '}']; |
| 137 | + unfold(input{i}, Namei, show); |
| 138 | + end |
124 | 139 | end |
125 | | - end |
126 | | - end |
127 | 140 |
|
128 | | - elseif iscell(input) |
129 | | - |
130 | | - if numel(input) == 0 |
131 | | - fprintf(' =\t{};'); |
132 | 141 | else |
133 | | - % recursively display cell |
134 | | - siz = size(input); |
135 | | - for i = 1:numel(input) |
136 | | - Namei = [Name '{' indToStr(siz, i) '}']; |
137 | | - unfold(input{i}, Namei, show); |
138 | | - end |
139 | | - end |
140 | 142 |
|
141 | | - else |
| 143 | + printKeyToScreen(Name); |
| 144 | + if show |
| 145 | + printValueToScreen(input); |
| 146 | + end |
142 | 147 |
|
143 | | - printKeyToScreen(Name); |
144 | | - if show |
145 | | - printValueToScreen(input); |
146 | 148 | end |
147 | 149 |
|
148 | | - end |
149 | | - |
150 | | - callStack = dbstack(); |
151 | | - if numel(callStack) > 1 && ~strcmp(callStack(2).name, mfilename()) |
152 | | - fprintf('\n'); |
153 | | - elseif numel(callStack) == 1 && strcmp(callStack(1).name, mfilename()) |
154 | | - fprintf('\n'); |
155 | | - end |
| 150 | + callStack = dbstack(); |
| 151 | + if numel(callStack) > 1 && ~strcmp(callStack(2).name, mfilename()) |
| 152 | + fprintf('\n'); |
| 153 | + elseif numel(callStack) == 1 && strcmp(callStack(1).name, mfilename()) |
| 154 | + fprintf('\n'); |
| 155 | + end |
156 | 156 |
|
157 | 157 | end |
158 | 158 |
|
159 | 159 | function printKeyToScreen(input) |
160 | | - fprintf(sprintf('\n%s', input)); |
| 160 | + fprintf(sprintf('\n%s', input)); |
161 | 161 | end |
162 | 162 |
|
163 | 163 | function printValueToScreen(input) |
164 | | - base_string = ' =\t'; |
165 | | - msg = ''; |
166 | | - if ischar(input) |
167 | | - msg = sprintf('%s''%s'' ', base_string, input); |
168 | | - elseif isinteger(input) || islogical(input) |
169 | | - if isempty(input) |
170 | | - msg = ' =\t[] '; |
171 | | - else |
172 | | - pattern = repmat('%i, ', 1, numel(input)); |
173 | | - msg = sprintf(['%s' pattern], base_string, input); |
174 | | - end |
175 | | - elseif isnumeric(input) |
176 | | - if isempty(input) |
177 | | - msg = ' =\t[] '; |
178 | | - else |
179 | | - pattern = repmat('%f, ', 1, numel(input)); |
180 | | - msg = sprintf(['%s' pattern], base_string, input); |
| 164 | + base_string = ' =\t'; |
| 165 | + msg = ''; |
| 166 | + if ischar(input) |
| 167 | + msg = sprintf('%s''%s'' ', base_string, input); |
| 168 | + elseif isinteger(input) || islogical(input) |
| 169 | + if isempty(input) |
| 170 | + msg = ' =\t[] '; |
| 171 | + else |
| 172 | + pattern = repmat('%i, ', 1, numel(input)); |
| 173 | + msg = sprintf(['%s' pattern], base_string, input); |
| 174 | + end |
| 175 | + elseif isnumeric(input) |
| 176 | + if isempty(input) |
| 177 | + msg = ' =\t[] '; |
| 178 | + else |
| 179 | + pattern = repmat('%f, ', 1, numel(input)); |
| 180 | + msg = sprintf(['%s' pattern], base_string, input); |
| 181 | + end |
181 | 182 | end |
182 | | - end |
183 | | - fprintf(msg); |
184 | | - fprintf('\b\b;'); |
| 183 | + fprintf(msg); |
| 184 | + fprintf('\b\b;'); |
185 | 185 | end |
186 | 186 |
|
187 | 187 | % local functions |
188 | 188 | % -------------------------------------------------------------------------- |
189 | 189 | function str = indToStr(siz, ndx) |
190 | 190 |
|
191 | | - n = length(siz); |
192 | | - % treat vectors and scalars correctly |
193 | | - if n == 2 |
194 | | - if siz(1) == 1 |
195 | | - siz = siz(2); |
196 | | - n = 1; |
197 | | - elseif siz(2) == 1 |
198 | | - siz = siz(1); |
199 | | - n = 1; |
| 191 | + n = length(siz); |
| 192 | + % treat vectors and scalars correctly |
| 193 | + if n == 2 |
| 194 | + if siz(1) == 1 |
| 195 | + siz = siz(2); |
| 196 | + n = 1; |
| 197 | + elseif siz(2) == 1 |
| 198 | + siz = siz(1); |
| 199 | + n = 1; |
| 200 | + end |
200 | 201 | end |
201 | | - end |
202 | | - k = [1 cumprod(siz(1:end - 1))]; |
203 | | - ndx = ndx - 1; |
204 | | - str = ''; |
205 | | - for i = n:-1:1 |
206 | | - v = floor(ndx / k(i)) + 1; |
207 | | - if i == n |
208 | | - str = num2str(v); |
209 | | - else |
210 | | - str = [num2str(v) ',' str]; |
| 202 | + k = [1 cumprod(siz(1:end - 1))]; |
| 203 | + ndx = ndx - 1; |
| 204 | + str = ''; |
| 205 | + for i = n:-1:1 |
| 206 | + v = floor(ndx / k(i)) + 1; |
| 207 | + if i == n |
| 208 | + str = num2str(v); |
| 209 | + else |
| 210 | + str = [num2str(v) ',' str]; |
| 211 | + end |
| 212 | + ndx = rem(ndx, k(i)); |
211 | 213 | end |
212 | | - ndx = rem(ndx, k(i)); |
213 | | - end |
214 | 214 |
|
215 | 215 | end |
0 commit comments