{"id":9667,"date":"2018-04-27T09:00:21","date_gmt":"2018-04-27T13:00:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9667"},"modified":"2018-04-23T17:37:03","modified_gmt":"2018-04-23T21:37:03","slug":"dir-tab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/04\/27\/dir-tab\/","title":{"rendered":"Dir Tab"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/66213\">dirtab(in)<\/a> by <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/72177\">Julian<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>Do you ever need to scrape the files in a directory and figure out how to parse them?  If you have, you&#8217;ve probably come across the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/dir.html\"><tt>dir<\/tt><\/a> command.  While <tt>dir<\/tt> works, it&#8217;s frustrating because you get back an array of structs, that, in releases since R2013b, would be better served by a table.  I&#8217;ve found that I now pretty much just always call <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/struct2table.html\"><tt>struct2table<\/tt><\/a> on the output.  See <a href=\"https:\/\/blogs.mathworks.com\/loren\/2018\/02\/14\/a-best-friend-struct2table\/\">Loren&#8217;s Blog Post<\/a> on this topic.\n   <\/p>\n<p>This is what Julian&#8217;s file does, but he goes one step further and converts the date to a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/datetime.html\"><tt>datetime<\/tt><\/a> rather than date number and removes those pesky &#8220;.&#8221; and &#8220;..&#8221;s from every listed directory.  Another bit of extra work saved.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dirtab(pwd)<\/pre>\n<pre style=\"font-style:oblique\">ans =\r\n  4&times;7 table\r\n                  folder                        name           ext              date            bytes    isdir     datenum  \r\n    __________________________________    ________________    ______    ____________________    _____    _____    __________\r\n    'C:\\Documents\\MATLAB\\potw\\dirtab\\'    'dirtab'            '.m'      23-Apr-2018 15:35:22    3776     false    7.3717e+05\r\n    'C:\\Documents\\MATLAB\\potw\\dirtab\\'    'license'           '.txt'    23-Apr-2018 15:57:12    1306     false    7.3717e+05\r\n    'C:\\Documents\\MATLAB\\potw\\dirtab\\'    'mainDirTab'        '.m'      23-Apr-2018 17:32:53    2154     false    7.3717e+05\r\n    'C:\\Documents\\MATLAB\\potw\\dirtab\\'    'wordpress_html'    ''        23-Apr-2018 17:33:13       0     true     7.3717e+05\r\n<\/pre>\n<p>However, since 18a came out, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/fullfile.html\"><tt>fullfile<\/tt><\/a> now works with <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/string.html\"><tt>strings<\/tt><\/a> (my favorite 18a feature!); I&#8217;ve typically gone a step further and converted the file parts to strings and added the full file path for easy opening.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">D = dirtab(pwd);\r\nSD = varfun(@convertCharsToStrings, D);\r\nSD.Properties.VariableNames = D.Properties.VariableNames;\r\nSD.fullfilepath = fullfile(SD.folder, SD.name + SD.ext);\r\nSD.datenum = []<\/pre>\n<pre style=\"font-style:oblique\">SD =\r\n  4&times;7 table\r\n                  folder                        name           ext              date            bytes    isdir                      fullfilepath                  \r\n    __________________________________    ________________    ______    ____________________    _____    _____    ________________________________________________\r\n    \"C:\\Documents\\MATLAB\\potw\\dirtab\\\"    \"dirtab\"            \".m\"      23-Apr-2018 15:35:22    3776     false    \"C:\\Documents\\MATLAB\\potw\\dirtab\\dirtab.m\"      \r\n    \"C:\\Documents\\MATLAB\\potw\\dirtab\\\"    \"license\"           \".txt\"    23-Apr-2018 15:57:12    1306     false    \"C:\\Documents\\MATLAB\\potw\\dirtab\\license.txt\"   \r\n    \"C:\\Documents\\MATLAB\\potw\\dirtab\\\"    \"mainDirTab\"        \".m\"      23-Apr-2018 17:32:53    2154     false    \"C:\\Documents\\MATLAB\\potw\\dirtab\\mainDirTab.m\"  \r\n    \"C:\\Documents\\MATLAB\\potw\\dirtab\\\"    \"wordpress_html\"    \"\"        23-Apr-2018 17:33:13       0     true     \"C:\\Documents\\MATLAB\\potw\\dirtab\\wordpress_html\"\r\n<\/pre>\n<h3>Comments<a name=\"3\"><\/a><\/h3>\n<p>Do you have other requirements you&#8217;d like to see in dir?<\/p>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9667#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/66213#comments\">comment<\/a> for Julian.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_1a9be0f56f2548df8c14fc2a22832315() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='1a9be0f56f2548df8c14fc2a22832315 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1a9be0f56f2548df8c14fc2a22832315';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<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\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_1a9be0f56f2548df8c14fc2a22832315()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2018a<\/p>\n<\/div>\n<p><!--\n1a9be0f56f2548df8c14fc2a22832315 ##### SOURCE BEGIN #####\n%% Dir Tab\n%\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/66213 dirtab(in)> by\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/profile\/authors\/72177 Julian>.\n% \n\n%% \n%\n% Do you ever need to scrape the files in a directory and figure out how to\n% parse them?  If you have, you've probably come across the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/dir.html\n% |dir|> command.  While |dir| works, it's frustrating because you get back\n% an array of structs, that, in releases since R2013b, would be better\n% served by a table.  I've found that I now pretty much just always call\n% <https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/struct2table.html\n% |struct2table|> on the output.  See <https:\/\/blogs.mathworks.com\/loren\/2018\/02\/14\/a-best-friend-struct2table\/ Loren's Blog Post> on this topic.\n%\n% This is what Julian's file does, but he goes one step further and\n% converts the date to a\n% <https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/datetime.html\n% |datetime|> rather than date number and removes those pesky \".\" and \"..\"s\n% from every listed directory.  Another bit of extra work saved.\n\ndirtab(pwd)\n\n%%\n% However, since 18a came out, <https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/fullfile.html |fullfile|> now works with\n% <https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/ref\/string.html\n% |strings|> (my favorite 18a feature!); I've typically gone a step further and\n% converted the file parts to strings and added the fullfile path for easy\n% opening.\n\nD = dirtab(fullfile(pwd, \"**\"));\nSD = varfun(@convertCharsToStrings, D);\nSD.Properties.VariableNames = D.Properties.VariableNames;\nSD.fullfilepath = fullfile(SD.folder, SD.name + SD.ext);\nSD.datenum = []\n\n\n%% Comments\n% Do you have other requirements you'd like to see in dir?\n%\n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=9667#respond here> or leave a\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/66213#comments\n% comment> for Julian.\n%\n \n\n##### SOURCE END ##### 1a9be0f56f2548df8c14fc2a22832315\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sean&#8216;s pick this week is dirtab(in) by Julian.<\/p>\n<p>Do you ever need to scrape the files in a directory and figure out how to parse them?  If you have, you&#8217;ve probably come&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/04\/27\/dir-tab\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"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\/9667"}],"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\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=9667"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9667\/revisions"}],"predecessor-version":[{"id":9677,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9667\/revisions\/9677"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}