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!
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.