File Exchange Pick of the Week

September 11th, 2009

Tables made easy

Jiro's pick this week is DataTable by Paul.

MATLAB stands for MATrix LABoratory. So it's no surprise that we work with a lot of matrices. From time to time, I've wanted to embed a matrix in a Wiki page or an HTML document. The problem is that it's a tedious process to create the appropriate markup, and it doesn't scale well for large matrices.

Paul's DataTable class makes this extremely simple. Just create your matrix like a cell array, and convert it to the appropriate markup with a single command. In addition, he uses the new MATLAB Classes (introduced in R2008a) to implement his class.

Let's take a look at an example. Oh, and here is some personal info about us.

table         = DataTable();
table{2:4, 1} = {'Bob'; 'Brett'; 'Jiro'};
table{1, 2:5} = {'Occupation', ...
  'MathWorker since...', ...
  'FEX submissions', ...
  'POTW posts'};
table{2, 2:5} = {'Quality Engineer', 'July 2001', 24, 25};
table{3, 2:5} = {'Application Engineer', 'Dec 2005', 47, 27};
table{4, 2:5} = {'Application Engineer', 'May 2006', 32, 23};

% Center-align the text
table.setColumnTextAlignment(1:5, 'c')

table.toText();
|       |      Occupation      | MathWorker since... | FEX submissions | POTW posts |
|  Bob  |   Quality Engineer   |      July 2001      |       24        |     25     |
| Brett | Application Engineer |      Dec 2005       |       47        |     27     |
| Jiro  | Application Engineer |      May 2006       |       32        |     23     |

For HTML printing, simply call the toHTML method. You can even add table attributes!

table.toHTML('tableattributes', 'bgcolor="#ffcc00"');
<table bgcolor="#ffcc00">
<tr>
  <td align="center"></td>
  <td align="center">Occupation</td>
  <td align="center">MathWorker since...</td>
  <td align="center">FEX submissions</td>
  <td align="center">POTW posts</td>
</tr>
<tr>
  <td align="center">Bob</td>
  <td align="center">Quality Engineer</td>
  <td align="center">July 2001</td>
  <td align="center">24</td>
  <td align="center">25</td>
</tr>
<tr>
  <td align="center">Brett</td>
  <td align="center">Application Engineer</td>
  <td align="center">Dec 2005</td>
  <td align="center">47</td>
  <td align="center">27</td>
</tr>
<tr>
  <td align="center">Jiro</td>
  <td align="center">Application Engineer</td>
  <td align="center">May 2006</td>
  <td align="center">32</td>
  <td align="center">23</td>
</tr>
</table>

The above HTML markup renders to the following table:

Occupation MathWorker since... FEX submissions POTW posts
Bob Quality Engineer July 2001 24 25
Brett Application Engineer Dec 2005 47 27
Jiro Application Engineer May 2006 32 23

Thanks for this very useful and well-written entry, Paul!

Comments?


Get the MATLAB code

Published with MATLAB® 7.9

One Response to “Tables made easy”

  1. Yair Altman replied on :

    Thanks Jiro

    I would also recommend Urs’s CPRINTF utility that formats input data of a multitude of formats into a configurable text table: http://www.mathworks.com/matlabcentral/fileexchange/23840

    Note that Urs’s CPRINTF should not be confused with my CPRINTF - they are entirely unrelated utilities which happen to have the same name due to very similar submission dates (I didn’t notice Urs’s submission when I submitted mine…).

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.