changeset 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 70184f6c6cb5 (current diff) 2c47bc9c8df4 (diff)
children f39f98b59f61 145b3b8c1e4e
files
diffstat 2 files changed, 123 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
diff -r 70184f6c6cb5 -r 354e40a8e1a5 latexTable.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/latexTable.m	Mon Sep 12 15:18:25 2016 +0200
@@ -0,0 +1,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
\ No newline at end of file
diff -r 70184f6c6cb5 -r 354e40a8e1a5 textTable.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/textTable.m	Mon Sep 12 15:18:25 2016 +0200
@@ -0,0 +1,64 @@
+
+% 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 textTable(data, leftColStrings, topRowStrings, dataFormat)
+
+    default_arg('dataFormat','%.2f')
+    
+    nRows = length(leftColStrings);
+    nCols = length(topRowStrings);
+    [m,n] = size(data);
+    
+    if(m ~= nRows || n ~=nCols)
+        error('Data dimensions must match labels');
+    end
+
+    % Find column widths
+    headerLength = 0;
+    for i = 1:nCols
+        headerLength = max(headerLength, length(topRowStrings{i} ));
+    end
+ 
+    dataLength = 0;
+    for i = 1:nRows
+        for j = 1:nCols
+            temp = length(sprintf(dataFormat, data{i,j}));
+            dataLength = max(dataLength, temp);
+        end
+    end
+    dataLength = length(sprintf(dataFormat, data{1,1}));
+    
+    colWidth = max(headerLength,dataLength);
+
+    % Print headers
+    fprintf(' %*s |',colWidth,'')
+    for i = 1:nCols
+        fprintf(' %-*s |', colWidth, topRowStrings{i});
+    end
+    fprintf('\n');
+
+    % Print divider
+    m_dev = repmat('-',1,colWidth);
+    column_dev = repmat('-',1,colWidth);
+    fprintf('-%s-+',m_dev);
+    for i = 1:nCols
+        fprintf('-%s-+', column_dev);
+    end
+    fprintf('\n');
+    
+
+    % Print each row
+    dataFormat = ['%*' dataFormat(2:end)];
+    for i = 1:nRows
+        fprintf(' %*s |',colWidth,leftColStrings{i});
+        for j = 1:nCols
+            fprintf([' ' dataFormat ' |'], colWidth, data{i,j});
+        end
+        fprintf('\n');
+    end
+
+    fprintf('\n');
+
+end
\ No newline at end of file