-
Notifications
You must be signed in to change notification settings - Fork 6
/
dprint.m
46 lines (41 loc) · 1.18 KB
/
dprint.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
function [ str ] = dprint( obj, d )
%DPRINT Deep print on any nested object.
% http://www.mathworks.com/help/matlab/data-type-identification.html
%#ok<*AGROW>
if nargin < 2
d = 0;
end
if ischar(obj)
str = sprintf('''%s''', obj);
elseif iscell(obj)
ss = {};
for i=1:length(obj)
ss{end+1} = dprint(obj{i}, d+1);
end
str = sprintf('{%s}', strjoin(ss, ', '));
elseif isa(obj, 'containers.Map')
keys = obj.keys;
ss = {};
for i=1:length(keys)
key = keys(i);
value = obj.values(key);
ss{end+1} = sprintf('%s:%s', dprint(key{1}, d+1), dprint(value{1}, d+1));
end
str = sprintf('#{%s}', strjoin(ss, ', '));
elseif isstruct(obj)
ss = {};
names = fieldnames(obj);
for i=1:length(names)
key = names{i};
value = getfield(obj, names{i});
ss{end+1} = sprintf('%s:%s', dprint(key, d+1), dprint(value, d+1));
end
str = sprintf('struct(%s)', strjoin(ss, ', '));
elseif isobject(obj)
str = evalc('disp(obj)');
elseif ismatrix(obj)
str = mat2str(obj, 5);
else
error('Type not supported.');
end
end