Get from Ico-github-logo

Highlights from
Printing Variables to HTML Tables in Published Code

  • Printing Variables to HTM...
  • makeHtmlTable(M, T, rowNa...
    MAKEHTMLTABLE Display matrix contents as an HTML table
  • View all files
image thumbnail

Printing Variables to HTML Tables in Published Code

by

 

28 Mar 2007 (Updated )

This file lets you display variables as HTML tables in your published MATLAB files.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Printing Variables to HTML Tables in Published Code

Printing Variables to HTML Tables in Published Code

One often-requested feature for Cell Mode publishing is to have a command that simply displays the output of a MATLAB® variable as a table. This file shows how to do that.

Contents

Passing through HTML

Beginning with R2007a, you can pass HTML directly through to your output file by wrapping it in an HTML tag.

Look at the difference between these two lines to see how it works.

disp('Option 1: This is <b>bold</b>')
Option 1: This is <b>bold</b>
disp('<html>Option 2: This is <b>bold</b></html>')
Option 2: This is bold

Here's a matrix...

m = magic(4)
m =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

which I can now display with the help of a function like makeHtmlTable.

makeHtmlTable(m);
162313
511108
97612
414151

Here is the help entry for makeHtmlTable

help makeHtmlTable
 MAKEHTMLTABLE  Display matrix contents as an HTML table
    makeHtmlTable(M) where M is an array
    makeHtmlTable(M, T) where T is a cell array of strings equal in size
    to M. The contents of T are displayed preferentially when T is not empty.
    makeHtmlTable(M, T, rowNames, colHeaders) where colHeaders and
    rowNames are cell arrays.
    makeHtmlTable(M, T, rowNames, colHeaders, colors) where colors is a
    standard three-column colormap. Color is scaled like in IMAGESC. NaNs
    in M are mapped to the color white.
    makeHtmlTable(M, T, rowNames, colHeaders, colors, strPrecision) where
    strPrecision specifies the digits of precision displayed as explained
    in MAT2STR.
 
    Example:
    makeHtmlTable([1 2; 3 4])

    Published output in the Help browser
       showdemo makeHtmlTable

Add column headers and row names

If you have them, we can show them.

r = {'row 1','row 2','row 3','row 4'};
c = {'col 1','col 2','col 3','col 4'};
makeHtmlTable(m,[],r,c);
col 1col 2col 3col 4
row 1162313
row 2511108
row 397612
row 4414151

Colorize the table cells

Here's a little hack for matrix visualization.

colorFlag = true;
cmap = gray;
cmap(:,1) = 1;
makeHtmlTable(m,[],r,c,cmap);
col 1col 2col 3col 4
row 1162313
row 2511108
row 397612
row 4414151

Some test cases

If all these run to completion, we're doing pretty well.

m = hilb(6);
makeHtmlTable(m,[],[],[],[],6);
10.50.3333330.250.20.166667
0.50.3333330.250.20.1666670.142857
0.3333330.250.20.1666670.1428570.125
0.250.20.1666670.1428570.1250.111111
0.20.1666670.1428570.1250.1111110.1
0.1666670.1428570.1250.1111110.10.0909091
cmap = jet/2 + 0.5;
makeHtmlTable(m,[],[],[],cmap,2);
10.50.330.250.20.17
0.50.330.250.20.170.14
0.330.250.20.170.140.13
0.250.20.170.140.130.11
0.20.170.140.130.110.1
0.170.140.130.110.10.091
m = [1 2; 3 4];
makeHtmlTable(m);
12
34
makeHtmlTable(m,[],[],[],summer);
12
34
makeHtmlTable(m,[],{'top','bottom'},{'left','right'},winter);
leftright
top12
bottom34
m = magic(11);
makeHtmlTable(m,[],[],[],autumn);
68819410712011427405366
809310611911132639526567
921051181012253851647779
10411792224375063767891
11682123364962758890103
72033354861748789102115
19323447607386991011146
31444659728598100113518
43455871849711011241730
55577083961091113162942
56698295108121215284154

If both the numeric input is NaN and the string input is non-empty for a given cell, then display the string.

m = magic(12);
t = cell(size(m));
m(23) = NaN;
t{23} = 'unknown';
m(79) = NaN;
t{79} = 'missing';
m(120) = NaN;
t{120} = 'no data';
makeHtmlTable(m,t,[],[],winter);
14423141140671371361011133
131311301617127126202112312224
251191182829115114323311111036
10838391051044243101100464797
965051939254558988585985
618382646579786869757472
737170767767missing8081636284
608687575690915352949549
4898994544102103414010610737
109353411211331301161172726120
121unknown2212412519181281291514132
121341359813813954no data1431

This displays the frequency with which ASCII characters show up in the help entry for the PRINT command.

t = num2cell(char(reshape(48:127,8,10)'));
m = zeros(size(t));
h = help('print');
for i = 1:numel(t)
    m(i) = sum(h==t{i});
end
makeHtmlTable(m,t,[],[],summer);
01234567
89:;<=>?
@ABCDEFG
HIJKLMNO
PQRSTUVW
XYZ[\]^_
`abcdefg
hijklmno
pqrstuvw
xyz{|}~

You can even use this to display images. Here's the full image.

load mandrill
image(X);
colormap(map)

Here's a zoomed sub-image of one eye.

c = 306;
cinc = 34;
r = 10;
rinc = 26;
X2 = X((1:rinc)+r, (1:cinc)+c);
image(X2);
colormap(map)
makeHtmlTable(X2,[],[],[],map);
2081630301958168988888198881681212101488684884
8481019101288191910668848166838888168888385
986111410861627141691168141610274553584775302953644720281810
101681910827816191558317064646464646410710715818113315818113310799107648199
148611146136563129708195999564701078181140158148133140170133181140148148133148133
66131127143356464816481107644053406410713317018117018117014818114817014814813314099
41110121267932814099871338199706481133133181133140148148129148148133148148148148129107
4894914354408199871339910714013399133140133140996481816481999912014814814814099
88613713729107431071338114081811331401331401338181648113310781816499148148133133140
6822168935881133811401489999148133148148133814029718151029154010799140148148148
687916853876481107170148991331401481331408164405317282210740819999133148
48126154536410713314013314813399991331409999644082775110282108256499133181
16141681775395871401331401701481071401339913310747811669472182202038281760107133148
667173125649515813314013399140133107181133140133474782216472182191791087156499140148
42716816896951401331401071481331071401331401336464155103701325354829819999170
83816614358140133107158148133140133140148148133814075102381181625232991076399
2121731575381107148133140133140107133133148140133958164881028152408199140133
34123177676481133107140991339914014814817018113314064641778817104060133140148170
414491897964641071401071339999991331401481481481331488164999540998199999912914899
4412168126208799819914813381140148148148148148148148148991331409914099140148133148129140
6127916867478181999914014813312912014814814817014817012914817014814813314814812914814899
448121371253281818113313314814814814814814814814816414814812014812918117014814814812914099
334124916869649910799140133140129129148121194148148148120181148148148148148129140999999
44681212379349910714099107991481291481811211811201481811481641481481709913344999999
449462813711664107133158140133140148148148170129140140148991701401339914081999999148
436244491371053881991331401331481481401489914813399133140133140107401071079999133

Contact us