Display Formatted Text Table of Data

Display data, mixed numeric and strings with optional col and row headers and your choice of col sep
1.8K Downloads
Updated 9 Apr 2013

View License

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

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

Cite As

Richard Crozier (2024). Display Formatted Text Table of Data (https://www.mathworks.com/matlabcentral/fileexchange/33717-display-formatted-text-table-of-data), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: Printing a formatted table

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.5.0.0

fixed a couple of bugs including Sanjay's

1.4.0.0

Minor bug fix and added further example to help

1.3.0.0

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

1.2.0.0

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.

1.1.0.0

No longer prints backspace character when printing to file.

1.0.0.0