-
Notifications
You must be signed in to change notification settings - Fork 0
/
objMatrixToString.m
48 lines (42 loc) · 1.1 KB
/
objMatrixToString.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
47
48
function str = objMatrixToString(obj,indent,spacing)
% objMatrixToString Create string representation of 2D object array.
%
% [STR] = objMatrixToString(OBJ)
% [STR] = objMatrixToString(OBJ,INDENT,SPACING)
%
% Convert an object array to a string using a grid layout. OBJ must
% have a valid toString method that will be called on each entry.
%
% Returns a single character array. All entries are right-aligned.
%
% INDENT is a string that appears at the start of each row.
% SPACING is a string that appears between each column.
if nargin < 2
indent = ' ';
end
if nargin < 3
spacing = ' ';
end
[m,n] = size(obj);
strs = cell(m,n);
for j = 1:n
for i = 1:m
strs{i,j} = toString(obj(i,j));
end
end
maxColLengths = zeros(1,n);
for j = 1:n
maxColLengths(j) = max(cellfun(@length,strs(:,j)));
end
for j = 1:n
for i = 1:m
len = length(strs{i,j});
padding = repmat(' ', 1, maxColLengths(j) - len);
strs{i,j} = [padding strs{i,j}];
end
end
rows = cell(1,m);
for i = 1:m
rows{i} = [indent strjoin(strs(i,:),spacing)];
end
str = strjoin(rows,'\n');