Mercurial > repos > public > sbplib
comparison TextTable.m @ 465:a5cebdaad10b
Implement a simple but general text table
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 01 Aug 2017 12:24:52 +0200 |
parents | textTable.m@fdee7f66a5e9 |
children | 11a39b274260 |
comparison
equal
deleted
inserted
replaced
458:283bdea202d2 | 465:a5cebdaad10b |
---|---|
1 classdef TextTable < handle | |
2 properties | |
3 data | |
4 fmtArray | |
5 vertDiv | |
6 horzDiv | |
7 | |
8 nCols | |
9 nRows | |
10 end | |
11 | |
12 methods | |
13 function obj = TextTable(data, vertDiv, horzDiv); | |
14 default_arg('vertDiv', []); | |
15 default_arg('horzDiv', []); | |
16 | |
17 | |
18 obj.data = data; | |
19 obj.vertDiv = vertDiv; | |
20 obj.horzDiv = horzDiv; | |
21 | |
22 [obj.nRows, obj.nCols] = size(data); | |
23 obj.fmtArray = cell(size(data)); | |
24 obj.formatAll('%s'); | |
25 | |
26 end | |
27 | |
28 function formatAll(obj, fmt) | |
29 obj.fmtArray(:,:) = {fmt}; | |
30 end | |
31 | |
32 function formatCell(obj, i, j, fmt) | |
33 obj.fmtArray{i,j} = fmt; | |
34 end | |
35 | |
36 function formatRow(obj, i, fmt) | |
37 obj.fmtArray(i,:) = {fmt}; | |
38 end | |
39 | |
40 function formatColumn(obj, j, fmt) | |
41 obj.fmtArray(:,j) = {fmt}; | |
42 end | |
43 | |
44 function widths = getWidths(obj) | |
45 strArray = obj.getStringArray(); | |
46 | |
47 widths = zeros(1,obj.nCols); | |
48 for j = 1:obj.nCols | |
49 for i = 1:obj.nRows | |
50 widths(j) = max(widths(j), length(strArray{i,j})); | |
51 end | |
52 end | |
53 end | |
54 | |
55 function str = toString(obj) | |
56 strArray = obj.getStringArray(); | |
57 widths = obj.getWidths(); | |
58 | |
59 str = ''; | |
60 | |
61 % First horzDiv | |
62 if ismember(0, obj.horzDiv) | |
63 str = [str, obj.getHorzDiv(widths)]; | |
64 end | |
65 | |
66 for i = 1:obj.nRows | |
67 str = [str, TextTable.rowToString(strArray(i,:), widths, obj.vertDiv, obj.horzDiv)]; | |
68 | |
69 % Interior horzDiv | |
70 if ismember(i, obj.horzDiv) | |
71 str = [str, obj.getHorzDiv(widths)]; | |
72 end | |
73 end | |
74 end | |
75 | |
76 function str = getHorzDiv(obj, widths) | |
77 str = TextTable.rowToString(cell(1,obj.nCols), widths, obj.vertDiv, obj.horzDiv); | |
78 str(find(' ' == str)) = '-'; | |
79 str(find('|' == str)) = '+'; | |
80 end | |
81 | |
82 function strArray = getStringArray(obj) | |
83 strArray = cell(size(obj.data)); | |
84 | |
85 for i = 1:obj.nRows | |
86 for j = 1:obj.nCols | |
87 strArray{i,j} = sprintf(obj.fmtArray{i,j}, obj.data{i,j}); | |
88 end | |
89 end | |
90 end | |
91 end | |
92 | |
93 methods (Static) | |
94 function str = rowToString(strs, widths, vertDiv, horzDiv) | |
95 % First vertDiv | |
96 if ismember(0, vertDiv) | |
97 str = '| '; | |
98 else | |
99 str = ' '; | |
100 end | |
101 | |
102 % Interior cols | |
103 for j = 1:length(strs) - 1 | |
104 str = [str, sprintf('%*s ', widths(j), strs{j})]; | |
105 | |
106 % Interior vertDiv | |
107 if ismember(j, vertDiv) | |
108 str = [str, '| ']; | |
109 end | |
110 end | |
111 | |
112 % Last col | |
113 str = [str, sprintf('%*s ', widths(end), strs{end})]; | |
114 | |
115 if ismember(length(strs), vertDiv) | |
116 str = [str, '|']; | |
117 end | |
118 | |
119 str = [str, sprintf('\n')]; | |
120 end | |
121 end | |
122 end |