comparison latexTable.m @ 289:354e40a8e1a5

Merged in feature/textTable (pull request #3) Feature/texttable
author Martin Almquist <martin.almquist@it.uu.se>
date Mon, 12 Sep 2016 15:18:25 +0200
parents 0b1580ed9b8b
children
comparison
equal deleted inserted replaced
285:70184f6c6cb5 289:354e40a8e1a5
1 % data -- cell array of numbers
2 % leftColstrings -- cell array of strings, for left column
3 % topRowStrings -- cell array of strings, for top row
4 % dataFormat -- (optional) format specifier, e.g. '%.2f'
5 function latexTable(data, leftColStrings, topRowStrings, dataFormat)
6
7 default_arg('dataFormat','%8.2f')
8
9 nRows = length(leftColStrings);
10 nCols = length(topRowStrings);
11 [m,n] = size(data);
12
13 if(m ~= nRows || n ~=nCols)
14 error('Data dimensions must match labels');
15 end
16
17 header = {
18 '\begin{table}[H]'
19 '\centering'
20 ['\begin{tabular}{c' repmat('|c',1,nCols) '} &']
21 headers(topRowStrings)
22 '\hline'
23 };
24
25 footer = {
26 '\end{tabular}'
27 '\caption{DESCRIPTION.}'
28 '\label{table:LABEL}'
29 '\end{table}'
30 };
31
32 nlc = sprintf('\n');
33 dataStr = '';
34 for i = 1:nRows
35 dataStr = [dataStr leftColStrings{i}]; %#ok<AGROW>
36 for j = 1:nCols
37 dataStr = [dataStr ' & ' sprintf(dataFormat,data{i,j}) ]; %#ok<AGROW>
38 end
39 if(i<nRows)
40 dataStr = [dataStr ' \\ ' nlc]; %#ok<AGROW>
41 end
42 end
43
44 header = strjoin(header', nlc);
45 footer = strjoin(footer', nlc);
46
47 table = strjoin({header, dataStr, footer}, nlc);
48 fprintf('%s\n', table);
49 end
50
51
52 function s = headers(strings)
53 s= [strings{1} ' '];
54 nCols = length(strings);
55 for i = 2:nCols
56 s = [s '& ' strings{i} ' ']; %#ok<AGROW>
57 end
58 s = [s ' \\'];
59 end