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 boldHere'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);
16 | 2 | 3 | 13 |
5 | 11 | 10 | 8 |
9 | 7 | 6 | 12 |
4 | 14 | 15 | 1 |
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 1 | col 2 | col 3 | col 4 | |
row 1 | 16 | 2 | 3 | 13 |
row 2 | 5 | 11 | 10 | 8 |
row 3 | 9 | 7 | 6 | 12 |
row 4 | 4 | 14 | 15 | 1 |
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 1 | col 2 | col 3 | col 4 | |
row 1 | 16 | 2 | 3 | 13 |
row 2 | 5 | 11 | 10 | 8 |
row 3 | 9 | 7 | 6 | 12 |
row 4 | 4 | 14 | 15 | 1 |
Some test cases
If all these run to completion, we're doing pretty well.
m = hilb(6); makeHtmlTable(m,[],[],[],[],6);
1 | 0.5 | 0.333333 | 0.25 | 0.2 | 0.166667 |
0.5 | 0.333333 | 0.25 | 0.2 | 0.166667 | 0.142857 |
0.333333 | 0.25 | 0.2 | 0.166667 | 0.142857 | 0.125 |
0.25 | 0.2 | 0.166667 | 0.142857 | 0.125 | 0.111111 |
0.2 | 0.166667 | 0.142857 | 0.125 | 0.111111 | 0.1 |
0.166667 | 0.142857 | 0.125 | 0.111111 | 0.1 | 0.0909091 |
cmap = jet/2 + 0.5; makeHtmlTable(m,[],[],[],cmap,2);
1 | 0.5 | 0.33 | 0.25 | 0.2 | 0.17 |
0.5 | 0.33 | 0.25 | 0.2 | 0.17 | 0.14 |
0.33 | 0.25 | 0.2 | 0.17 | 0.14 | 0.13 |
0.25 | 0.2 | 0.17 | 0.14 | 0.13 | 0.11 |
0.2 | 0.17 | 0.14 | 0.13 | 0.11 | 0.1 |
0.17 | 0.14 | 0.13 | 0.11 | 0.1 | 0.091 |
m = [1 2; 3 4]; makeHtmlTable(m);
1 | 2 |
3 | 4 |
makeHtmlTable(m,[],[],[],summer);
1 | 2 |
3 | 4 |
makeHtmlTable(m,[],{'top','bottom'},{'left','right'},winter);
left | right | |
top | 1 | 2 |
bottom | 3 | 4 |
m = magic(11); makeHtmlTable(m,[],[],[],autumn);
68 | 81 | 94 | 107 | 120 | 1 | 14 | 27 | 40 | 53 | 66 |
80 | 93 | 106 | 119 | 11 | 13 | 26 | 39 | 52 | 65 | 67 |
92 | 105 | 118 | 10 | 12 | 25 | 38 | 51 | 64 | 77 | 79 |
104 | 117 | 9 | 22 | 24 | 37 | 50 | 63 | 76 | 78 | 91 |
116 | 8 | 21 | 23 | 36 | 49 | 62 | 75 | 88 | 90 | 103 |
7 | 20 | 33 | 35 | 48 | 61 | 74 | 87 | 89 | 102 | 115 |
19 | 32 | 34 | 47 | 60 | 73 | 86 | 99 | 101 | 114 | 6 |
31 | 44 | 46 | 59 | 72 | 85 | 98 | 100 | 113 | 5 | 18 |
43 | 45 | 58 | 71 | 84 | 97 | 110 | 112 | 4 | 17 | 30 |
55 | 57 | 70 | 83 | 96 | 109 | 111 | 3 | 16 | 29 | 42 |
56 | 69 | 82 | 95 | 108 | 121 | 2 | 15 | 28 | 41 | 54 |
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);
144 | 2 | 3 | 141 | 140 | 6 | 7 | 137 | 136 | 10 | 11 | 133 |
13 | 131 | 130 | 16 | 17 | 127 | 126 | 20 | 21 | 123 | 122 | 24 |
25 | 119 | 118 | 28 | 29 | 115 | 114 | 32 | 33 | 111 | 110 | 36 |
108 | 38 | 39 | 105 | 104 | 42 | 43 | 101 | 100 | 46 | 47 | 97 |
96 | 50 | 51 | 93 | 92 | 54 | 55 | 89 | 88 | 58 | 59 | 85 |
61 | 83 | 82 | 64 | 65 | 79 | 78 | 68 | 69 | 75 | 74 | 72 |
73 | 71 | 70 | 76 | 77 | 67 | missing | 80 | 81 | 63 | 62 | 84 |
60 | 86 | 87 | 57 | 56 | 90 | 91 | 53 | 52 | 94 | 95 | 49 |
48 | 98 | 99 | 45 | 44 | 102 | 103 | 41 | 40 | 106 | 107 | 37 |
109 | 35 | 34 | 112 | 113 | 31 | 30 | 116 | 117 | 27 | 26 | 120 |
121 | unknown | 22 | 124 | 125 | 19 | 18 | 128 | 129 | 15 | 14 | 132 |
12 | 134 | 135 | 9 | 8 | 138 | 139 | 5 | 4 | no data | 143 | 1 |
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);
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | : | ; | < | = | > | ? |
@ | A | B | C | D | E | F | G |
H | I | J | K | L | M | N | O |
P | Q | R | S | T | U | V | W |
X | Y | Z | [ | \ | ] | ^ | _ |
` | a | b | c | d | e | f | g |
h | i | j | k | l | m | n | o |
p | q | r | s | t | u | v | w |
x | y | z | { | | | } | ~ | |
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);
20 | 8 | 16 | 30 | 30 | 19 | 5 | 8 | 16 | 8 | 9 | 8 | 8 | 8 | 8 | 8 | 19 | 8 | 8 | 8 | 16 | 8 | 12 | 12 | 10 | 14 | 8 | 8 | 6 | 8 | 4 | 8 | 8 | 4 |
8 | 4 | 8 | 10 | 19 | 10 | 12 | 8 | 8 | 19 | 19 | 10 | 6 | 6 | 8 | 8 | 4 | 8 | 16 | 6 | 8 | 3 | 8 | 8 | 8 | 8 | 16 | 8 | 8 | 8 | 8 | 3 | 8 | 5 |
9 | 8 | 6 | 11 | 14 | 10 | 8 | 6 | 16 | 27 | 14 | 16 | 9 | 11 | 6 | 8 | 14 | 16 | 10 | 27 | 45 | 53 | 58 | 47 | 75 | 30 | 29 | 53 | 64 | 47 | 20 | 28 | 18 | 10 |
10 | 16 | 8 | 19 | 10 | 8 | 27 | 8 | 16 | 19 | 15 | 58 | 31 | 70 | 64 | 64 | 64 | 64 | 64 | 64 | 107 | 107 | 158 | 181 | 133 | 158 | 181 | 133 | 107 | 99 | 107 | 64 | 81 | 99 |
14 | 8 | 6 | 11 | 14 | 61 | 36 | 56 | 31 | 29 | 70 | 81 | 95 | 99 | 95 | 64 | 70 | 107 | 81 | 81 | 140 | 158 | 148 | 133 | 140 | 170 | 133 | 181 | 140 | 148 | 148 | 133 | 148 | 133 |
6 | 6 | 13 | 11 | 27 | 143 | 35 | 64 | 64 | 81 | 64 | 81 | 107 | 64 | 40 | 53 | 40 | 64 | 107 | 133 | 170 | 181 | 170 | 181 | 170 | 148 | 181 | 148 | 170 | 148 | 148 | 133 | 140 | 99 |
4 | 11 | 10 | 12 | 126 | 79 | 32 | 81 | 40 | 99 | 87 | 133 | 81 | 99 | 70 | 64 | 81 | 133 | 133 | 181 | 133 | 140 | 148 | 148 | 129 | 148 | 148 | 133 | 148 | 148 | 148 | 148 | 129 | 107 |
4 | 8 | 9 | 49 | 143 | 54 | 40 | 81 | 99 | 87 | 133 | 99 | 107 | 140 | 133 | 99 | 133 | 140 | 133 | 140 | 99 | 64 | 81 | 81 | 64 | 81 | 99 | 99 | 120 | 148 | 148 | 148 | 140 | 99 |
8 | 8 | 6 | 137 | 137 | 29 | 107 | 43 | 107 | 133 | 81 | 140 | 81 | 81 | 133 | 140 | 133 | 140 | 133 | 81 | 81 | 64 | 81 | 133 | 107 | 81 | 81 | 64 | 99 | 148 | 148 | 133 | 133 | 140 |
6 | 8 | 22 | 168 | 93 | 58 | 81 | 133 | 81 | 140 | 148 | 99 | 99 | 148 | 133 | 148 | 148 | 133 | 81 | 40 | 29 | 7 | 18 | 15 | 10 | 29 | 15 | 40 | 107 | 99 | 140 | 148 | 148 | 148 |
6 | 8 | 79 | 168 | 53 | 87 | 64 | 81 | 107 | 170 | 148 | 99 | 133 | 140 | 148 | 133 | 140 | 81 | 64 | 40 | 53 | 17 | 2 | 8 | 2 | 2 | 10 | 7 | 40 | 81 | 99 | 99 | 133 | 148 |
4 | 8 | 126 | 154 | 53 | 64 | 107 | 133 | 140 | 133 | 148 | 133 | 99 | 99 | 133 | 140 | 99 | 99 | 64 | 40 | 8 | 2 | 7 | 75 | 110 | 28 | 2 | 10 | 8 | 25 | 64 | 99 | 133 | 181 |
16 | 14 | 168 | 177 | 53 | 95 | 87 | 140 | 133 | 140 | 170 | 148 | 107 | 140 | 133 | 99 | 133 | 107 | 47 | 81 | 16 | 69 | 47 | 218 | 220 | 203 | 8 | 2 | 8 | 17 | 60 | 107 | 133 | 148 |
6 | 67 | 173 | 125 | 64 | 95 | 158 | 133 | 140 | 133 | 99 | 140 | 133 | 107 | 181 | 133 | 140 | 133 | 47 | 47 | 82 | 216 | 47 | 218 | 219 | 179 | 10 | 8 | 7 | 15 | 64 | 99 | 140 | 148 |
4 | 27 | 168 | 168 | 96 | 95 | 140 | 133 | 140 | 107 | 148 | 133 | 107 | 140 | 133 | 140 | 133 | 64 | 64 | 15 | 5 | 10 | 3 | 70 | 132 | 53 | 5 | 4 | 8 | 29 | 81 | 99 | 99 | 170 |
8 | 38 | 166 | 143 | 58 | 140 | 133 | 107 | 158 | 148 | 133 | 140 | 133 | 140 | 148 | 148 | 133 | 81 | 40 | 75 | 10 | 2 | 3 | 8 | 118 | 16 | 2 | 5 | 2 | 32 | 99 | 107 | 63 | 99 |
2 | 12 | 173 | 157 | 53 | 81 | 107 | 148 | 133 | 140 | 133 | 140 | 107 | 133 | 133 | 148 | 140 | 133 | 95 | 81 | 64 | 8 | 8 | 10 | 2 | 8 | 1 | 5 | 2 | 40 | 81 | 99 | 140 | 133 |
3 | 4 | 123 | 177 | 67 | 64 | 81 | 133 | 107 | 140 | 99 | 133 | 99 | 140 | 148 | 148 | 170 | 181 | 133 | 140 | 64 | 64 | 17 | 7 | 8 | 8 | 17 | 10 | 40 | 60 | 133 | 140 | 148 | 170 |
4 | 14 | 49 | 189 | 79 | 64 | 64 | 107 | 140 | 107 | 133 | 99 | 99 | 99 | 133 | 140 | 148 | 148 | 148 | 133 | 148 | 81 | 64 | 99 | 95 | 40 | 99 | 81 | 99 | 99 | 99 | 129 | 148 | 99 |
4 | 4 | 12 | 168 | 126 | 20 | 87 | 99 | 81 | 99 | 148 | 133 | 81 | 140 | 148 | 148 | 148 | 148 | 148 | 148 | 148 | 148 | 99 | 133 | 140 | 99 | 140 | 99 | 140 | 148 | 133 | 148 | 129 | 140 |
6 | 1 | 2 | 79 | 168 | 67 | 47 | 81 | 81 | 99 | 99 | 140 | 148 | 133 | 129 | 120 | 148 | 148 | 148 | 170 | 148 | 170 | 129 | 148 | 170 | 148 | 148 | 133 | 148 | 148 | 129 | 148 | 148 | 99 |
4 | 4 | 8 | 12 | 137 | 125 | 32 | 81 | 81 | 81 | 133 | 133 | 148 | 148 | 148 | 148 | 148 | 148 | 148 | 148 | 164 | 148 | 148 | 120 | 148 | 129 | 181 | 170 | 148 | 148 | 148 | 129 | 140 | 99 |
3 | 3 | 4 | 12 | 49 | 168 | 69 | 64 | 99 | 107 | 99 | 140 | 133 | 140 | 129 | 129 | 148 | 121 | 194 | 148 | 148 | 148 | 120 | 181 | 148 | 148 | 148 | 148 | 148 | 129 | 140 | 99 | 99 | 99 |
4 | 4 | 6 | 8 | 12 | 123 | 79 | 34 | 99 | 107 | 140 | 99 | 107 | 99 | 148 | 129 | 148 | 181 | 121 | 181 | 120 | 148 | 181 | 148 | 164 | 148 | 148 | 170 | 99 | 133 | 44 | 99 | 99 | 99 |
4 | 4 | 9 | 4 | 6 | 28 | 137 | 116 | 64 | 107 | 133 | 158 | 140 | 133 | 140 | 148 | 148 | 148 | 170 | 129 | 140 | 140 | 148 | 99 | 170 | 140 | 133 | 99 | 140 | 81 | 99 | 99 | 99 | 148 |
4 | 3 | 6 | 2 | 4 | 4 | 49 | 137 | 105 | 38 | 81 | 99 | 133 | 140 | 133 | 148 | 148 | 140 | 148 | 99 | 148 | 133 | 99 | 133 | 140 | 133 | 140 | 107 | 40 | 107 | 107 | 99 | 99 | 133 |