{"id":2688,"date":"2018-02-14T09:05:47","date_gmt":"2018-02-14T14:05:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=2688"},"modified":"2018-02-21T15:13:55","modified_gmt":"2018-02-21T20:13:55","slug":"a-best-friend-struct2table","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2018\/02\/14\/a-best-friend-struct2table\/","title":{"rendered":"A Best Friend, struct2table"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Sometimes it's nice to have a best friend.  Sometimes I have more than one, context-dependent.  I want to share my best friend today for dealing with <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\">struct<\/a><\/tt> arrays.<\/p><p>Structure arrays are a useful way to collect similar information for a collection of items, even if what gets collected is not always the same size.  However, struct arrays can also be cumbersome to work with. Thanks to the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/tables.html\">table<\/a><\/tt> datatype, I can now avoid some of the intricacies of indexing into struct arrays by simply performing a conversion to <tt>table<\/tt> first, and then using good, old-fashioned, stands the test of time, MATLAB indexing.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#26f673c6-ee3a-4758-ae4b-1e08007b96a2\">Some examples<\/a><\/li><li><a href=\"#aca8fbb7-05b8-48c9-92b6-637816be44aa\">Let's try it<\/a><\/li><li><a href=\"#4a7120b9-b042-465a-8a3b-81898e75abd1\">Have you replaced your use of struct arrays with tables?<\/a><\/li><\/ul><\/div><h4>Some examples<a name=\"26f673c6-ee3a-4758-ae4b-1e08007b96a2\"><\/a><\/h4><p>Some examples for working with struct arrays after converting to a table include:<\/p><div><ol><li>output of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dir.html\"><tt>dir<\/tt><\/a> - (I can then remove dirs, etc. very easily)<\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/jsondecode.html\"><tt>jsondecode<\/tt><\/a><\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/regionprops.html\"><tt>regionprops<\/tt><\/a> from <a href=\"https:\/\/www.mathworks.com\/products\/image.html\">Image Processing Toolbox<\/a>  - (now includes output to <tt>table<\/tt> option)<\/li><\/ol><\/div><h4>Let's try it<a name=\"aca8fbb7-05b8-48c9-92b6-637816be44aa\"><\/a><\/h4><p>I'll now get the listing for the blog post directory for one of my guest authors, Alan Weiss.<\/p><pre class=\"codeinput\">dirstruct = dir(<span class=\"string\">'AlanW'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">dirstruct = \r\n  5&times;1 struct array with fields:\r\n    name\r\n    folder\r\n    date\r\n    bytes\r\n    isdir\r\n    datenum\r\n<\/pre><p>And here's a picture from the Windows Explorer<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/alanWposts.png\" alt=\"\"> <\/p><p>You can readily see that I really have only 3 items, and not the 5 suggested in <tt>dirstruct<\/tt>.  I can, of course, now look at the names.  I extract them via a comma-separated list.<\/p><pre class=\"codeinput\">dirstruct.name\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n    '.'\r\nans =\r\n    '..'\r\nans =\r\n    'Clumping'\r\nans =\r\n    'FinSymbolic'\r\nans =\r\n    'Sudoku'\r\n<\/pre><p>and now you can see that I really don't care about the first two.  But I'd have to delete these from the name field, and the folder field, and ...  So I definitely can do so, but what a hassle.<\/p><p>Instead let me convert to a <tt>table<\/tt>.<\/p><pre class=\"codeinput\">dirtable = struct2table(dirstruct)\r\n<\/pre><pre class=\"codeoutput\">dirtable =\r\n  5&times;6 table\r\n        name                   folder                        date             bytes    isdir     datenum  \r\n    _____________    ___________________________    ______________________    _____    _____    __________\r\n    '.'              'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    '..'             'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    'Clumping'       'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    'FinSymbolic'    'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    'Sudoku'         'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n<\/pre><p>And now I can eliminate the first 2 rows in the usual way in MATLAB:<\/p><pre class=\"codeinput\">dirtable(1:2,:) = []\r\n<\/pre><pre class=\"codeoutput\">dirtable =\r\n  3&times;6 table\r\n        name                   folder                        date             bytes    isdir     datenum  \r\n    _____________    ___________________________    ______________________    _____    _____    __________\r\n    'Clumping'       'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    'FinSymbolic'    'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n    'Sudoku'         'C:\\Work\\ArtofMATLAB\\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05\r\n<\/pre><h4>Have you replaced your use of struct arrays with tables?<a name=\"4a7120b9-b042-465a-8a3b-81898e75abd1\"><\/a><\/h4><p>I'm wondering if you've used struct arrays before and are now replacing them with tables.  And if you have an application where that does not make sense, I'd love to hear about that as well.  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=2688#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d8ad2adc6af846f687a9daff06a3f731() {\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='d8ad2adc6af846f687a9daff06a3f731 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d8ad2adc6af846f687a9daff06a3f731';\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        copyright = 'Copyright 2018 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_d8ad2adc6af846f687a9daff06a3f731()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><\/div><!--\r\nd8ad2adc6af846f687a9daff06a3f731 ##### SOURCE BEGIN #####\r\n%% A Best Friend, struct2table\r\n% Sometimes it's nice to have a best friend.  Sometimes I have more than\r\n% one, context-dependent.  I want to share my best friend today for dealing\r\n% with |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html struct>|\r\n% arrays.  \r\n%\r\n% Structure arrays are a useful way to collect similar information\r\n% for a collection of items, even if what gets collected is not always the\r\n% same size.  However, struct arrays can also be cumbersome to work with.\r\n% Thanks to the |<https:\/\/www.mathworks.com\/help\/matlab\/tables.html table>|\r\n% datatype, I can now avoid some of the intricacies of indexing into struct\r\n% arrays by simply performing a conversion to |table| first, and then\r\n% using good, old-fashioned, stands the test of time, MATLAB indexing.\r\n\r\n%% Some examples\r\n% Some examples for working with struct arrays after converting to a table\r\n% include:\r\n%\r\n% # output of <https:\/\/www.mathworks.com\/help\/matlab\/ref\/dir.html |dir|> - (I can then remove dirs, etc. very easily)\r\n% # <https:\/\/www.mathworks.com\/help\/matlab\/ref\/jsondecode.html |jsondecode|>\r\n% # <https:\/\/www.mathworks.com\/help\/images\/ref\/regionprops.html |regionprops|> from <https:\/\/www.mathworks.com\/products\/image.html Image\r\n% Processing Toolbox>  - (now includes output to |table| option)\r\n\r\n%% Let's try it\r\n% I'll now get the listing for the blog post directory for one of my guest\r\n% authors, Alan Weiss.\r\ndirstruct = dir('AlanW')\r\n%%\r\n% And here's a picture from the Windows Explorer\r\n% \r\n% <<alanWposts.png>>\r\n%\r\n% You can readily see that I really have only 3 items, and not the 5\r\n% suggested in |dirstruct|.  I can, of course, now look at the names.  I\r\n% extract them via a comma-separated list.\r\ndirstruct.name\r\n%%\r\n% and now you can see that I really don't care about the first two.  But\r\n% I'd have to delete these from the name field, and the folder field, and\r\n% ...  So I definitely can do so, but what a hassle.\r\n%%\r\n% Instead let me convert to a |table|.\r\ndirtable = struct2table(dirstruct)\r\n%%\r\n% And now I can eliminate the first 2 rows in the usual way in MATLAB:\r\ndirtable(1:2,:) = []\r\n%% Have you replaced your use of struct arrays with tables?\r\n% I'm wondering if you've used struct arrays before and are now replacing\r\n% them with tables.  And if you have an application where that does not\r\n% make sense, I'd love to hear about that as well.  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=2688#respond here>.\r\n\r\n##### SOURCE END ##### d8ad2adc6af846f687a9daff06a3f731\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/alanWposts.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Sometimes it's nice to have a best friend.  Sometimes I have more than one, context-dependent.  I want to share my best friend today for dealing with <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\">struct<\/a><\/tt> arrays.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2018\/02\/14\/a-best-friend-struct2table\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2688"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=2688"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2688\/revisions"}],"predecessor-version":[{"id":2718,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2688\/revisions\/2718"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=2688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=2688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=2688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}