ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
show.m
Go to the documentation of this file.
1%> \brief
2%> Display the components of an input MATLAB variable on MATLAB Console recursively.
3%>
4%> \details
5%> This function is particularly useful for displaying the
6%> hierarchical contents of a MATLAB ``struct`` or ``cell`` object.
7%>
8%> \param[in] obj : The input variable whose contents are to be displayed on MATLAB Console.
9%> <ol>
10%> <li> If the input ``obj`` is a MATLAB ``struct``, the function will
11%> recursively show the ``obj`` name, its fieldnames, and their contents.
12%> <li> If the input ``obj`` is a cell array, the contents of each cell are displayed.
13%> </ol>
14%> (**optional**. default = ``[]``, effectively adding a new line to the command line.)
15%> \param[in] name : The input scalar MATLAB string, representing the actual name of the input ``obj``.<br>
16%> (**optional**. If missing, the ``obj`` variable name from the caller workspace is used.)
17%> \param[in] hidden : The input scalar MATLAB ``logical``.<br>
18%> If ``true``, then the contents of the input ``struct`` will not be shown, only the field names.<br>
19%> (**optional**, default = ``false``)
20%>
21%> \interface{show}
22%> \code{.m}
23%>
24%> pm.matlab.show();
25%> pm.matlab.show([]);
26%> pm.matlab.show(obj);
27%> pm.matlab.show(obj, name);
28%> pm.matlab.show(obj, [], hidden);
29%> pm.matlab.show(obj, name, hidden);
30%>
31%> \endcode
32%>
33%> \example{show}
34%> \include{lineno} example/matlab/show/main.m
35%> \output{show}
36%> \include{lineno} example/matlab/show/main.out.m
37%>
38%> \final{show}
39%>
40%> \author
41%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
42%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
43function show(obj, name, hidden)
44
45 if nargin < 3
46 hidden = [];
47 end
48 if nargin < 2
49 name = [];
50 end
51 if nargin < 1
52 fprintf('\n');
53 return;
54 end
55
56 if isempty(hidden)
57 hidden = false;
58 end
59 if isempty(name)
60 name = inputname(1);
61 end
62 name = convertStringsToChars(name);
63
64 if isstruct(obj)
65
66 %%%% The number of elements to be displayed.
67
68 objsize = numel(obj);
69 if ~hidden
70 hmax = objsize;
71 else
72 hmax = min(1, objsize);
73 end
74
75 %%%% Recursively display structure including fieldnames.
76
77 for h = 1 : hmax
78 fnames = fieldnames(obj(h));
79 fnamesCount = length(fnames);
80 for i = 1 : fnamesCount
81 if objsize > 1
82 siz = size(obj);
83 if ~hidden
84 namei = [name '(' ind2str(siz, h) ').' fnames{i}];
85 else
86 namei = [name '(' ind2str(siz, objsize) ').' fnames{i}];
87 end
88 else
89 namei = [name '.' fnames{i}];
90 end
91 if isstruct(obj(h).(fnames{i}))
92 pm.matlab.show(obj(h).(fnames{i}), namei, hidden);
93 else
94 if iscell(obj(h).(fnames{i}))
95 siz = size(obj(h).(fnames{i}));
96 NC = numel(obj(h).(fnames{i}));
97 if ~hidden
98 jmax = NC;
99 else
100 jmax = 1;
101 end
102 for j = 1 : jmax
103 if ~hidden
104 Namej = [namei '{' ind2str(siz,j) '}'];
105 else
106 Namej = [namei '{' ind2str(siz,NC) '}'];
107 end
108 disp(Namej);
109 if ~hidden
110 disp(obj(h).(fnames{i}){j});
111 end
112 end
113 else
114 disp(namei);
115 if ~hidden
116 disp(obj(h).(fnames{i}));
117 end
118 end
119 end
120 end
121 end
122
123 elseif iscell(obj)
124
125 %%%% Recursively display cell.
126
127 siz = size(obj);
128 for i = 1 : numel(obj)
129 namei = [name '{' ind2str(siz,i) '}'];
130 pm.matlab.show(obj{i}, namei, hidden);
131 end
132
133 else
134
135 disp(name);
136 if ~hidden
137 disp(obj)
138 end
139
140 end
141
142end
143
144%> \cond excluded
145
146function str = ind2str(siz, ndx)
147 %%%% Treat vectors and scalars correctly.
148 n = length(siz);
149 if n == 2
150 if siz(1) == 1
151 siz = siz(2);
152 n = 1;
153 elseif siz(2) == 1
154 siz = siz(1);
155 n = 1;
156 end
157 end
158 k = [1 cumprod(siz(1 : end - 1))];
159 ndx = ndx - 1;
160 str = '';
161 for i = n : -1 : 1
162 v = floor(ndx / k(i)) + 1;
163 if i == n
164 str = num2str(v);
165 else
166 str = [num2str(v) ',' str];
167 end
168 ndx = rem(ndx, k(i));
169 end
170end
171
172%> \endcond excluded
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function show(in obj, in name, in hidden)
Display the components of an input MATLAB variable on MATLAB Console recursively.
excluded
Definition: show.m:173