comparison textTable.m @ 353:fd4f1c80755d feature/hypsyst

meged with default
author Ylva Rydin <ylva.rydin@telia.com>
date Thu, 10 Nov 2016 20:49:25 +0100
parents fdee7f66a5e9
children
comparison
equal deleted inserted replaced
352:9b3d7fc61a36 353:fd4f1c80755d
1
2 % data -- cell array of numbers
3 % leftColstrings -- cell array of strings, for left column
4 % topRowStrings -- cell array of strings, for top row
5 % dataFormat -- (optional) format specifier, e.g. '%.2f'
6 function textTable(data, leftColStrings, topRowStrings, dataFormat)
7
8 default_arg('dataFormat','%.2f')
9
10 nRows = length(leftColStrings);
11 nCols = length(topRowStrings);
12 [m,n] = size(data);
13
14 if(m ~= nRows || n ~=nCols)
15 error('Data dimensions must match labels');
16 end
17
18 % Find column widths
19 headerLength = 0;
20 for i = 1:nCols
21 headerLength = max(headerLength, length(topRowStrings{i} ));
22 end
23
24 dataLength = 0;
25 for i = 1:nRows
26 for j = 1:nCols
27 temp = length(sprintf(dataFormat, data{i,j}));
28 dataLength = max(dataLength, temp);
29 end
30 end
31 dataLength = length(sprintf(dataFormat, data{1,1}));
32
33 colWidth = max(headerLength,dataLength);
34
35 % Print headers
36 fprintf(' %*s |',colWidth,'')
37 for i = 1:nCols
38 fprintf(' %-*s |', colWidth, topRowStrings{i});
39 end
40 fprintf('\n');
41
42 % Print divider
43 m_dev = repmat('-',1,colWidth);
44 column_dev = repmat('-',1,colWidth);
45 fprintf('-%s-+',m_dev);
46 for i = 1:nCols
47 fprintf('-%s-+', column_dev);
48 end
49 fprintf('\n');
50
51
52 % Print each row
53 dataFormat = ['%*' dataFormat(2:end)];
54 for i = 1:nRows
55 fprintf(' %*s |',colWidth,leftColStrings{i});
56 for j = 1:nCols
57 fprintf([' ' dataFormat ' |'], colWidth, data{i,j});
58 end
59 fprintf('\n');
60 end
61
62 fprintf('\n');
63
64 end