{"id":2466,"date":"2009-09-11T11:49:58","date_gmt":"2009-09-11T11:49:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/11\/tables-made-easy\/"},"modified":"2009-09-11T11:49:58","modified_gmt":"2009-09-11T11:49:58","slug":"tables-made-easy","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/11\/tables-made-easy\/","title":{"rendered":"Tables made easy"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24766-datatable\"><tt>DataTable<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/55230\">Paul<\/a>.\r\n   <\/p>\r\n   <p>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\r\n      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\r\n      markup, and it doesn't scale well for large matrices.\r\n   <\/p>\r\n   <p>Paul's <tt>DataTable<\/tt> class makes this extremely simple. Just create your matrix like a cell array, and convert it to the appropriate markup with\r\n      a single command. In addition, he uses the new <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_oop\/brh2rgw.html\">MATLAB Classes<\/a> (introduced in R2008a) to implement his class.\r\n   <\/p>\r\n   <p>Let's take a look at an example. Oh, and here is some personal info about us.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">table         = DataTable();\r\ntable{2:4, 1} = {<span style=\"color: #A020F0\">'Bob'<\/span>; <span style=\"color: #A020F0\">'Brett'<\/span>; <span style=\"color: #A020F0\">'Jiro'<\/span>};\r\ntable{1, 2:5} = {<span style=\"color: #A020F0\">'Occupation'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n  <span style=\"color: #A020F0\">'MathWorker since...'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n  <span style=\"color: #A020F0\">'FEX submissions'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n  <span style=\"color: #A020F0\">'POTW posts'<\/span>};\r\ntable{2, 2:5} = {<span style=\"color: #A020F0\">'Quality Engineer'<\/span>, <span style=\"color: #A020F0\">'July 2001'<\/span>, 24, 25};\r\ntable{3, 2:5} = {<span style=\"color: #A020F0\">'Application Engineer'<\/span>, <span style=\"color: #A020F0\">'Dec 2005'<\/span>, 47, 27};\r\ntable{4, 2:5} = {<span style=\"color: #A020F0\">'Application Engineer'<\/span>, <span style=\"color: #A020F0\">'May 2006'<\/span>, 32, 23};\r\n\r\n<span style=\"color: #228B22\">% Center-align the text<\/span>\r\ntable.setColumnTextAlignment(1:5, <span style=\"color: #A020F0\">'c'<\/span>)\r\n\r\ntable.toText();<\/pre><pre style=\"font-style:oblique\">\r\n|       |      Occupation      | MathWorker since... | FEX submissions | POTW posts |\r\n|  Bob  |   Quality Engineer   |      July 2001      |       24        |     25     |\r\n| Brett | Application Engineer |      Dec 2005       |       47        |     27     |\r\n| Jiro  | Application Engineer |      May 2006       |       32        |     23     |\r\n\r\n<\/pre><p>For HTML printing, simply call the <tt>toHTML<\/tt> method. You can even add table attributes!\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">table.toHTML(<span style=\"color: #A020F0\">'tableattributes'<\/span>, <span style=\"color: #A020F0\">'bgcolor=\"#ffcc00\"'<\/span>);<\/pre><pre style=\"font-style:oblique\">&lt;table bgcolor=\"#ffcc00\"&gt;\r\n&lt;tr&gt;\r\n  &lt;td align=\"center\"&gt;&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;Occupation&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;MathWorker since...&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;FEX submissions&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;POTW posts&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;tr&gt;\r\n  &lt;td align=\"center\"&gt;Bob&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;Quality Engineer&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;July 2001&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;24&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;25&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;tr&gt;\r\n  &lt;td align=\"center\"&gt;Brett&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;Application Engineer&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;Dec 2005&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;47&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;27&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;tr&gt;\r\n  &lt;td align=\"center\"&gt;Jiro&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;Application Engineer&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;May 2006&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;32&lt;\/td&gt;\r\n  &lt;td align=\"center\"&gt;23&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n<\/pre><p>The above HTML markup renders to the following table:<\/p>\r\n   <p>\r\n      <table bgcolor=\"#ffcc00\">\r\n      <tr>\r\n      <td align=\"center\"><\/td>\r\n      <td align=\"center\">Occupation<\/td>\r\n      <td align=\"center\">MathWorker since...<\/td>\r\n      <td align=\"center\">FEX submissions<\/td>\r\n      <td align=\"center\">POTW posts<\/td>\r\n      <\/tr>\r\n      <tr>\r\n      <td align=\"center\">Bob<\/td>\r\n      <td align=\"center\">Quality Engineer<\/td>\r\n      <td align=\"center\">July 2001<\/td>\r\n      <td align=\"center\">24<\/td>\r\n      <td align=\"center\">25<\/td>\r\n      <\/tr>\r\n      <tr>\r\n      <td align=\"center\">Brett<\/td>\r\n      <td align=\"center\">Application Engineer<\/td>\r\n      <td align=\"center\">Dec 2005<\/td>\r\n      <td align=\"center\">47<\/td>\r\n      <td align=\"center\">27<\/td>\r\n      <\/tr>\r\n      <tr>\r\n      <td align=\"center\">Jiro<\/td>\r\n      <td align=\"center\">Application Engineer<\/td>\r\n      <td align=\"center\">May 2006<\/td>\r\n      <td align=\"center\">32<\/td>\r\n      <td align=\"center\">23<\/td>\r\n      <\/tr>\r\n      <\/table>\r\n      \r\n   <\/p>\r\n   <p>Thanks for this very useful and well-written entry, Paul!<\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2466#respond\">Comments<\/a>?\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6e2ebc7f68264149838410d832dd1dad() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='6e2ebc7f68264149838410d832dd1dad ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6e2ebc7f68264149838410d832dd1dad';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_6e2ebc7f68264149838410d832dd1dad()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\n6e2ebc7f68264149838410d832dd1dad ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24766-datatable\r\n% |DataTable|> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/55230\r\n% Paul>.\r\n%\r\n% MATLAB stands for MATrix LABoratory. So it's no surprise that we work\r\n% with a lot of matrices. From time to time, I've wanted to embed a matrix\r\n% in a Wiki page or an HTML document. The problem is that it's a tedious\r\n% process to create the appropriate markup, and it doesn't scale well for\r\n% large matrices.\r\n%\r\n% Paul's |DataTable| class makes this extremely simple. Just create your\r\n% matrix like a cell array, and convert it to the appropriate markup with a\r\n% single command. In addition, he uses the new\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_oop\/brh2rgw.html \r\n% MATLAB Classes> (introduced in R2008a) to implement his class.\r\n%\r\n% Let's take a look at an example. Oh, and here is some personal info\r\n% about us.\r\n\r\ntable         = DataTable();\r\ntable{2:4, 1} = {'Bob'; 'Brett'; 'Jiro'};\r\ntable{1, 2:5} = {'Occupation', ...\r\n  'MathWorker since...', ...\r\n  'FEX submissions', ...\r\n  'POTW posts'};\r\ntable{2, 2:5} = {'Quality Engineer', 'July 2001', 24, 25};\r\ntable{3, 2:5} = {'Application Engineer', 'Dec 2005', 47, 27};\r\ntable{4, 2:5} = {'Application Engineer', 'May 2006', 32, 23};\r\n\r\n% Center-align the text\r\ntable.setColumnTextAlignment(1:5, 'c')\r\n\r\ntable.toText();\r\n\r\n%%\r\n% For HTML printing, simply call the |toHTML| method. You can even add\r\n% table attributes!\r\n\r\ntable.toHTML('tableattributes', 'bgcolor=\"#ffcc00\"');\r\n\r\n%%\r\n% The above HTML markup renders to the following table:\r\n%\r\n% <html>\r\n% <table bgcolor=\"#ffcc00\">\r\n% <tr>\r\n%   <td align=\"center\"><\/td>\r\n%   <td align=\"center\">Occupation<\/td>\r\n%   <td align=\"center\">MathWorker since...<\/td>\r\n%   <td align=\"center\">FEX submissions<\/td>\r\n%   <td align=\"center\">POTW posts<\/td>\r\n% <\/tr>\r\n% <tr>\r\n%   <td align=\"center\">Bob<\/td>\r\n%   <td align=\"center\">Quality Engineer<\/td>\r\n%   <td align=\"center\">July 2001<\/td>\r\n%   <td align=\"center\">24<\/td>\r\n%   <td align=\"center\">25<\/td>\r\n% <\/tr>\r\n% <tr>\r\n%   <td align=\"center\">Brett<\/td>\r\n%   <td align=\"center\">Application Engineer<\/td>\r\n%   <td align=\"center\">Dec 2005<\/td>\r\n%   <td align=\"center\">47<\/td>\r\n%   <td align=\"center\">27<\/td>\r\n% <\/tr>\r\n% <tr>\r\n%   <td align=\"center\">Jiro<\/td>\r\n%   <td align=\"center\">Application Engineer<\/td>\r\n%   <td align=\"center\">May 2006<\/td>\r\n%   <td align=\"center\">32<\/td>\r\n%   <td align=\"center\">23<\/td>\r\n% <\/tr>\r\n% <\/table>\r\n% <\/html>\r\n%\r\n%\r\n% Thanks for this very useful and well-written entry, Paul!\r\n%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2466#respond Comments>?\r\n##### SOURCE END ##### 6e2ebc7f68264149838410d832dd1dad\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is DataTable by Paul.\r\n   \r\n   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\r\n      to embed... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/11\/tables-made-easy\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2466"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2466"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2466\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}