Code covered by the BSD License  

Highlights from
Display Formatted Text Table of Data

5.0

5.0 | 2 ratings Rate this file 30 Downloads (last 30 days) File Size: 4.37 KB File ID: #33717

Display Formatted Text Table of Data

by Richard Crozier

 

10 Nov 2011 (Updated 27 Feb 2012)

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 submission has inspired the following:
Printing a formatted table
Required Products MATLAB
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
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.

03 Feb 2012 Richard Crozier

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

20 Feb 2012 grega

Thank you for the great func!

Please login to add a comment or rating.
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

Tag Activity for this File
Tag Applied By Date/Time
format Richard Crozier 10 Nov 2011 11:39:09
text Richard Crozier 10 Nov 2011 11:39:09
output Richard Crozier 10 Nov 2011 11:39:09
table Richard Crozier 10 Nov 2011 11:39:09

Contact us at files@mathworks.com