-
Notifications
You must be signed in to change notification settings - Fork 2
/
latexTable.m
59 lines (49 loc) · 1.48 KB
/
latexTable.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
49
50
51
52
53
54
55
56
57
58
59
% data -- cell array of numbers
% leftColstrings -- cell array of strings, for left column
% topRowStrings -- cell array of strings, for top row
% dataFormat -- (optional) format specifier, e.g. '%.2f'
function latexTable(data, leftColStrings, topRowStrings, dataFormat)
default_arg('dataFormat','%8.2f')
nRows = length(leftColStrings);
nCols = length(topRowStrings);
[m,n] = size(data);
if(m ~= nRows || n ~=nCols)
error('Data dimensions must match labels');
end
header = {
'\begin{table}[H]'
'\centering'
['\begin{tabular}{c' repmat('|c',1,nCols) '} &']
headers(topRowStrings)
'\hline'
};
footer = {
'\end{tabular}'
'\caption{DESCRIPTION.}'
'\label{table:LABEL}'
'\end{table}'
};
nlc = sprintf('\n');
dataStr = '';
for i = 1:nRows
dataStr = [dataStr leftColStrings{i}]; %#ok<AGROW>
for j = 1:nCols
dataStr = [dataStr ' & ' sprintf(dataFormat,data{i,j}) ]; %#ok<AGROW>
end
if(i<nRows)
dataStr = [dataStr ' \\ ' nlc]; %#ok<AGROW>
end
end
header = strjoin(header', nlc);
footer = strjoin(footer', nlc);
table = strjoin({header, dataStr, footer}, nlc);
fprintf('%s\n', table);
end
function s = headers(strings)
s= [strings{1} ' '];
nCols = length(strings);
for i = 2:nCols
s = [s '& ' strings{i} ' ']; %#ok<AGROW>
end
s = [s ' \\'];
end