Code covered by the BSD License  

Highlights from
Display Formatted Text Table of Data

5.0

5.0 | 6 ratings Rate this file 42 Downloads (last 30 days) File Size: 4.4 KB File ID: #33717

Display Formatted Text Table of Data

by

 

10 Nov 2011 (Updated )

Display data, mixed numeric and strings with optional col and row headers and your choice of col sep

| Watch this File

File Information
Description

Prints formatted matrix of numerical data with headings

Syntax

displaytable(data, colheadings, wid, fms, rowheadings, fid, colsep, rowending)

Input

data: a matrix or cell array, containing the data to be put in the table.
If a matrix the numbers in the table will be printed using the default
format specifier (f), or the specifier(s) supplied in fms. data can also
be a cell array containing a mixture of strings and numbers. each cell in
this case can contain only a single scalar value. In this case numbers
are printed as in the matrix case, while strings are printed up to the
maximum width of the column.

colheadings: a cell array of strings for the headings of each column. Can
be an empty cell array if no headings are required. If no column widths
are supplied (see below), the columns will be made wide enough to
accomdate the headings.

wid: (optional) scalar or vector of column widths to use for the table.
If scalar, every column will have the same width. If a vector it must be
of the same length as the number of columns of data. If not supplied, and
column headers are supplied, the width of the widest column header will
be used. If not supplied and column headers are not supplied, a default
with of 16 characters is used.

fms: (optional) a string, or cell array of strings containing format
specifiers for formatting the numerical output in each column. If a
single string, the same specifier is used for every column. If not
supplied, the 'g' specifier is used for every column.

rowheadings: (optional) a cell array of strings for the start of each
row. Can be an empty cell array if no row headings are required. If row
headings are supplied, the first column will be made wide enough to
accomodate all the headings.

fid: (optional) the file id to print to. Use 1 for stdout (to print to
the command line).

colsep: (optional) A string or character to insert between every column.
The default separation string is ' | ', i.e. a space followed by a
vertical bar, followed by a space. A table suitible for inclusion in a
LaTeX document can be created using the ' & ' string, for example.

rowending: (optional) An optional string or character to be appended at
the end of every row. Default is an empty string.

Example 1 - Basic useage

colheadings = {'number of projects','sales','profit'};
rowheadings = {'Jimmy Slick', 'Norman Noob'}
data = [3 rand(1) rand(1); 1 rand(1) rand(1)];

To format the first number in each row as a decimal (%d), the second
number %16.4f, and the third as %16.5E do the following:

wid = 16;
fms = {'d','.4f','.5E'};

In this case 16 will be the field width, and '.5E' is what to use for the
fms argument

fileID = 1;

>> displaytable(data,colheadings,wid,fms,rowheadings,fileID);
            |number of projec | sales | profit
Jimmy Slick | 3 | 0.4502 | 5.22908E-001
Norman Noob | 1 | 0.9972 | 2.78606E-002

Example 2 - Produce a latex table

colheadings = {'number of projects','sales','profit'};
rowheadings = {'Jimmy Slick', 'Norman Noob'};
data = [3 rand(1) rand(1); 1 rand(1) rand(1)];
wid = 16;
fms = {'d'};

colsep = ' & ';
rowending = ' \\';

fileID = 1;

>> displaytable(data,colheadings,wid,fms,rowheadings,fileID,colsep,rowending);

            & number of projec & sales & profit \\
Jimmy Slick & 3 & 6.948286e-001 & 3.170995e-001 \\
Norman Noob & 1 & 9.502220e-001 & 3.444608e-002 \\

Example 3 - Mixed numeric and strings

colheadings = {'number of projects','sales','profit'};
rowheadings = {'Jimmy Slick', 'Norman Noob'};

data = {3, rand(1), rand(1);
        1, 'none', 0};
    
wid = 16;
fms = {'d','.4f','.5E'};

fileID = 1;

>> displaytable(data,colheadings,wid,fms,rowheadings,fileID);
            | number of projec | sales | profit
Jimmy Slick | 3 | 0.4854 | 8.00280E-001
Norman Noob | 1 | none | 0.00000E+000

*******************

Unfortunately the file exchange does not use a fixed width font, hence the ugly example output above

*******************

Acknowledgements

This file inspired Printing A Formatted Table.

Required Products MATLAB
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
02 Jul 2013 Noah

It blows my mind that some kind of similar functionality is not already built in to Matlab. Thanks for putting this together!

12 Apr 2013 Richard Crozier

@arnold,

I'm not seeing this error on either 64 Bit Linux with R2011a or Win 32 R2012a, is this the exact code you ran?

12 Apr 2013 arnold

Hi Richard, I get this error when executing your example 2, any idea why?

====================================
>> colheadings = {'number of projects','sales','profit'};
rowheadings = {'Jimmy Slick', 'Norman Noob'};
data = [3 rand(1) rand(1); 1 rand(1) rand(1)];
wid = 16;
fms = {'d'};

colsep = ' & ';
rowending = ' \\';

fileID = 1;

displaytable(data,colheadings,wid,fms,rowheadings,fileID,colsep,rowending);
Maximum recursion limit of 500 reached. Use
set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your
available stack space can
crash MATLAB and/or your computer.

Error in displaytable

08 Apr 2013 Sanjay Manohar

Could you update this bug:

line 338

str = sprintf(['%',num2str(wid(j)),'g'],data(i,j));

should be

str = sprintf(['%',num2str(wid(j)),'g'],data{i,j});

Thanks for the useful script.
Sanjay

08 Apr 2013 Sanjay Manohar

Could you update this bug:

line 338

str = sprintf(['%',num2str(wid(j)),'g'],data(i,j));

should be

str = sprintf(['%',num2str(wid(j)),'g'],data{i,j});

Thanks for the useful script.
Sanjay

19 Mar 2013 Soumyadipta Sarkar

Great function. Much needed one. Please add some word recognition features so that the colheadings and rowheadings can be wraped around for a fixed width in a meaningful way.

20 Feb 2012 grega

Thank you for the great func!

03 Feb 2012 Richard Crozier

Thanks Andrew, I have updated the file with your fix.

03 Feb 2012 Andrew Davis

Works great, exactly what I wanted for quick table output. My only criticism is it prints a backspace character to the file if you're not using fid=1, so I added "&& fid == 1" to the conditional statements on lines 219 and 273.

Updates
03 Feb 2012

No longer prints backspace character when printing to file.

21 Feb 2012

Improved how column widths are determined, by taking more account of decimal places. Changed the behaviour of the column sep specifier. Also changed how it is determined not to place column sep at end of row to better method.

23 Feb 2012

Fixed bug related to row endings. Added latex table example.

27 Feb 2012

Minor bug fix and added further example to help

09 Apr 2013

fixed a couple of bugs including Sanjay's

Contact us