1 | function table(superheader,header,data,formats) |
---|
2 | %TABLE Internal function to display tables |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: table.m,v 1.2 2004/07/02 08:17:32 johanl Exp $ |
---|
6 | |
---|
7 | |
---|
8 | [nheadersy,nheadersx] = size(header); |
---|
9 | [ndatay,ndatax] = size(data); |
---|
10 | |
---|
11 | datasizes = zeros(ndatay,ndatax); |
---|
12 | for i = 1:ndatay |
---|
13 | for j = 1:ndatax |
---|
14 | if isa(data{i,j},'double') |
---|
15 | data{i,j} = num2str(data{i,j}); |
---|
16 | end |
---|
17 | datasizes(i,j) = length(data{i,j}); |
---|
18 | end |
---|
19 | end |
---|
20 | |
---|
21 | headersizes = zeros(1,nheadersx); |
---|
22 | for j = 1:nheadersx |
---|
23 | if isa(header{j},'double') |
---|
24 | header{j} = num2str(header{j}); |
---|
25 | end |
---|
26 | headersizes(1,j) = length(header{j}); |
---|
27 | end |
---|
28 | |
---|
29 | if nargin<4 |
---|
30 | for i = 1:ndatax |
---|
31 | formats{i}.header.just = 'right'; |
---|
32 | formats{i}.data.just = 'right'; |
---|
33 | end |
---|
34 | end |
---|
35 | |
---|
36 | datawidth = sum(datasizes,2); |
---|
37 | |
---|
38 | MaxWidth = max([headersizes;datasizes]); |
---|
39 | HeaderLine = ['|']; |
---|
40 | for i = 1:nheadersx |
---|
41 | HeaderLine = [HeaderLine ' ' strjust(fillstringRight(header{i},MaxWidth(i)+2),formats{i}.header.just) '|']; |
---|
42 | end |
---|
43 | HeaderLine = [HeaderLine '']; |
---|
44 | |
---|
45 | for j = 1:ndatay |
---|
46 | DataLine{j} = ['|']; |
---|
47 | for i = 1:ndatax |
---|
48 | DataLine{j} = [DataLine{j} ' ' strjust(fillstringRight(data{j,i},MaxWidth(i)+2),formats{i}.data.just) '|']; |
---|
49 | end |
---|
50 | end |
---|
51 | if ~isempty(superheader) |
---|
52 | disp(char(repmat(double('+'),1,length(HeaderLine)))) |
---|
53 | disp(['|' strjust(fillstringLeft(superheader{1},length(HeaderLine)-2),'center') '|']) |
---|
54 | end |
---|
55 | disp(char(repmat(double('+'),1,length(HeaderLine)))) |
---|
56 | disp(HeaderLine) |
---|
57 | disp(char(repmat(double('+'),1,length(HeaderLine)))) |
---|
58 | for i = 1:length(DataLine) |
---|
59 | disp(DataLine{i}); |
---|
60 | end |
---|
61 | disp(char(repmat(double('+'),1,length(HeaderLine)))) |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | function x= truncstring(x,n) |
---|
66 | if length(x) > n |
---|
67 | x = [x(1:n-3) '...']; |
---|
68 | end |
---|
69 | |
---|
70 | function x = fillstringLeft(x,n) |
---|
71 | x = [x blanks(n-length(x))]; |
---|
72 | |
---|
73 | function x = fillstringRight(x,n) |
---|
74 | x = [blanks(n-length(x)) x]; |
---|
75 | |
---|
76 | |
---|
77 | |
---|