-
Notifications
You must be signed in to change notification settings - Fork 0
/
repr.m
39 lines (37 loc) · 1.07 KB
/
repr.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
function s = repr(obj, numPrecision)
% REPR get string representation of input. Input may be numeric, logical, a string, a cell array, or
% a struct. Objects are converted to structs, keeping field names and values.
%
% Copyright (c) 2018 Richard Lange
if isobject(obj)
obj = struct(obj);
end
if isnumeric(obj)
if isscalar(obj)
s = num2str(obj, numPrecision);
else
s = ['[' strjoin(arrayfun(@(num) num2str(num, numPrecision), obj, 'UniformOutput', false), '-') ']'];
end
elseif ischar(obj)
s = strrep(obj(:)', ' ', '_');
elseif islogical(obj)
if obj
s = 'T';
else
s = 'F';
end
elseif iscell(obj)
s = ['{' strjoin(cellfun(@(sub) repr(sub, numPrecision), obj, 'UniformOutput', false), '-') '}'];
elseif isstruct(obj)
fields = fieldnames(obj);
sParts = cell(size(fields));
for i=1:length(fields)
key = fields{i};
val = obj.(key);
sParts{i} = [key '=' repr(val, numPrecision)];
end
s = ['(' strjoin(sParts, '-') ')'];
elseif isa(obj, 'function_handle')
s = [func2str(obj) '()'];
end
end