Mercurial > repos > public > sbplib
diff textTable.m @ 286:fdee7f66a5e9 feature/textTable
plainTextTable working.
author | Martin Almquist <martin.almquist@it.uu.se> |
---|---|
date | Mon, 12 Sep 2016 14:10:40 +0200 |
parents | 3c25601f1fe3 |
children |
line wrap: on
line diff
--- a/textTable.m Mon Sep 05 16:41:13 2016 +0200 +++ b/textTable.m Mon Sep 12 14:10:40 2016 +0200 @@ -1,67 +1,60 @@ -% data -- cell array of strings for each cell -function textTable(data,divCol, divColWeight, divRow, divRowWeight) +% 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) - % Find column widths - colWidth = {}; - for i = 1:size(data, 2) - for j = 1:size(data, 1) - c(j) = length(data{j,i}); - end - colWidth{i} = max(c); + 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 - error('not done') - - eW = 0; - qW = 0; - for i = 1:length(orders) - log_e{i} = log10(e{i}); - eW = max(eW, findFieldWidth('%.2f',log_e{i})); - qW = max(qW, findFieldWidth('%.2f',q{i})); + % 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); - mW = findFieldWidth('%d',m); - orderHeaderWidth = eW + qW + 1; - - fprintf('method: %s\nT: %d\n',methodName, T); - - % Print order headers - fprintf(' %*s |',mW,'') - for i = 1:length(orders) - fprintf(' %-*s |', orderHeaderWidth, sprintf('Order %d', orders{i})); + % Print headers + fprintf(' %*s |',colWidth,'') + for i = 1:nCols + fprintf(' %-*s |', colWidth, topRowStrings{i}); end fprintf('\n'); - - % Print eq headers - fprintf(' %*s |',mW,'m'); - for i = 1:length(orders) - fprintf(' %*s %*s |', eW, 'e', qW, 'q'); - end - fprintf('\n'); - - - % Print devider - m_dev = repmat('-',1,mW); - column_dev = repmat('-',1,orderHeaderWidth); + % Print divider + m_dev = repmat('-',1,colWidth); + column_dev = repmat('-',1,colWidth); fprintf('-%s-+',m_dev); - for i = 1:length(orders) + for i = 1:nCols fprintf('-%s-+', column_dev); end fprintf('\n'); - - + % Print each row - for i = 1:length(m) - fprintf(' %*d |',mW,m(i)); - for j = 1:length(orders) - if i == 1 - fprintf(' %*.2f %*s |', eW, log_e{j}(i), qW, ''); - else - fprintf(' %*.2f %*.2f |', eW, log_e{j}(i), qW, q{j}(i-1)); - end + 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