|
| 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); |
| 69 | + end |
| 70 | + |
| 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 |
| 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 | + |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + elseif iscell(input) |
| 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 |
| 139 | + end |
| 140 | + |
| 141 | + else |
| 142 | + |
| 143 | + printKeyToScreen(Name); |
| 144 | + if show |
| 145 | + printValueToScreen(input); |
| 146 | + end |
| 147 | + |
| 148 | + end |
| 149 | + |
| 150 | +end |
| 151 | + |
| 152 | +function printKeyToScreen(input) |
| 153 | + fprintf(sprintf('\n%s', input)); |
| 154 | +end |
| 155 | + |
| 156 | +function printValueToScreen(input) |
| 157 | + base_string = ' =\t'; |
| 158 | + if ischar(input) |
| 159 | + msg = sprintf('%s''%s'' ', base_string, input); |
| 160 | + elseif isinteger(input) || islogical(input) |
| 161 | + if isempty(input) |
| 162 | + msg = ' =\t[] '; |
| 163 | + else |
| 164 | + pattern = repmat('%i, ', 1, numel(input)); |
| 165 | + msg = sprintf(['%s' pattern], base_string, input); |
| 166 | + end |
| 167 | + elseif isnumeric(input) |
| 168 | + if isempty(input) |
| 169 | + msg = ' =\t[] '; |
| 170 | + else |
| 171 | + pattern = repmat('%f, ', 1, numel(input)); |
| 172 | + msg = sprintf(['%s' pattern], base_string, input); |
| 173 | + end |
| 174 | + end |
| 175 | + fprintf(msg); |
| 176 | + fprintf('\b\b;'); |
| 177 | +end |
| 178 | + |
| 179 | +% local functions |
| 180 | +% -------------------------------------------------------------------------- |
| 181 | +function str = indToStr(siz, ndx) |
| 182 | + |
| 183 | + n = length(siz); |
| 184 | + % treat vectors and scalars correctly |
| 185 | + if n == 2 |
| 186 | + if siz(1) == 1 |
| 187 | + siz = siz(2); |
| 188 | + n = 1; |
| 189 | + elseif siz(2) == 1 |
| 190 | + siz = siz(1); |
| 191 | + n = 1; |
| 192 | + end |
| 193 | + end |
| 194 | + k = [1 cumprod(siz(1:end - 1))]; |
| 195 | + ndx = ndx - 1; |
| 196 | + str = ''; |
| 197 | + for i = n:-1:1 |
| 198 | + v = floor(ndx / k(i)) + 1; |
| 199 | + if i == n |
| 200 | + str = num2str(v); |
| 201 | + else |
| 202 | + str = [num2str(v) ',' str]; |
| 203 | + end |
| 204 | + ndx = rem(ndx, k(i)); |
| 205 | + end |
| 206 | + |
| 207 | +end |
0 commit comments