{"id":2465,"date":"2009-09-18T14:21:19","date_gmt":"2009-09-18T14:21:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/18\/easier-and-less-error-prone-creation-of-zip-files\/"},"modified":"2009-09-18T14:21:19","modified_gmt":"2009-09-18T14:21:19","slug":"easier-and-less-error-prone-creation-of-zip-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/18\/easier-and-less-error-prone-creation-of-zip-files\/","title":{"rendered":"Easier (and less error-prone) creation of Zip files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/10702-exporttozip--identifies-the-dependencies-of-an-m-file-and-creates-a-zip-file\"><tt>exportToZip<\/tt><\/a>, by fellow MathWorker <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/21286\">Malcolm Wood<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>I recently received an email from a File Exchange user informing me that a GUI I had shared for morphologically processing\r\n      images (i.e., <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23697-image-morphology\"><tt>morphTool<\/tt><\/a>) didn't work. As it turns out, I had neglected to include in the Zip file that I uploaded a couple of functions that were\r\n      called internally by <tt>morphTool<\/tt>.\r\n   <\/p>\r\n   <p>I've made the same mistake before, and I've also downloaded File Exchange files that were missing some key functionality.<\/p>\r\n   <p>This email exchange had me thinking about writing a bit of code to automatically create my Zip files, making sure to include\r\n      all necessary supporting function files. MATLAB has a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/depfun.html\"><tt>depfun<\/tt><\/a> command that will <i>thoroughly<\/i> analyze a function and determine its dependencies, including, by default, functions in MATLAB Toolboxes. It can take a little\r\n      while to generate a report, though, and does a lot more work than is necessary just to create a comprehensive Zip file. Alternatively,\r\n      one can easily <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/rn\/f3-998197.html#f3-1007592\">create a dependency report<\/a> for the current file active in the MATLAB editor by using \"Save and Show Dependency Report\" from the Tools menu. That approach\r\n      is much faster than using <tt>depfun<\/tt> (with its default options), but leaves you then to manually evaluate one-by-one each function that your top-level function\r\n      calls. As you might guess, it's easy to miss a necessary file when you create your Zip.\r\n   <\/p>\r\n   <p>Before I started coding, I thought I'd check the File Exchange (wouldn't you?), and I quickly found Malcolm's <tt>exportToZip<\/tt>. Malcolm's file uses his own version of <tt>depfun<\/tt> (called <tt>mydepfun<\/tt>), that smartly uses non-default behavior of <tt>depfun<\/tt> to automatically skip files in MATLAB Toolboxes; <tt>mydepfun<\/tt> returns the paths to just those files needed for the target Zip file, which is then automatically created.\r\n   <\/p>\r\n   <p>I tried <tt>exportToZip<\/tt> on <tt>morphTool<\/tt>; it worked flawlessly--and quickly! And syntactically, it couldn't be easier to use:\r\n   <\/p>\r\n   <p><tt>zipfilename = exportToZip(funcname,zipfilename)<\/tt><\/p>\r\n   <p>Oh, and incidentally, a new version of <tt>morphTool<\/tt> will go live soon. Thanks, Malcolm--you saved me a lot of time and effort!\r\n   <\/p>\r\n   <p>You gotta love the File Exchange!<\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2465#respond\">Comments?<\/a><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_dbc53bc2eb94469eb54a9fc2b703639c() {\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='dbc53bc2eb94469eb54a9fc2b703639c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dbc53bc2eb94469eb54a9fc2b703639c';\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 = 'Brett Shoelson';\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_dbc53bc2eb94469eb54a9fc2b703639c()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\ndbc53bc2eb94469eb54a9fc2b703639c ##### SOURCE BEGIN #####\r\n%% Easier (and less error-prone) creation of Zip files\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s\r\n% Pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/10702-exporttozip--identifies-the-dependencies-of-an-m-file-and-creates-a-zip-file |exportToZip|>,\r\n% by fellow MathWorker <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/21286 Malcolm Wood>.\r\n\r\n%%\r\n% I recently received an email from a File Exchange user informing me that a GUI I\r\n% had shared for morphologically processing images (i.e., <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23697-image-morphology |morphTool|>) didn't work.\r\n% As it turns out, I had neglected to \r\n% include in the Zip file that I uploaded a couple of functions that were called internally by |morphTool|.\r\n\r\n%% \r\n% I've made the same mistake before, and I've also downloaded File Exchange files that were missing some key functionality.\r\n\r\n%% \r\n% This email exchange had me thinking about writing a bit of code to\r\n% automatically create my Zip files, making sure to include all necessary\r\n% supporting function files. MATLAB has a <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/depfun.html |depfun|>\r\n% command that will _thoroughly_ analyze a function and determine its dependencies,\r\n% including, by default, functions in MATLAB Toolboxes. It can take a little while to generate a report, though, and does a lot more work than is necessary\r\n% just to create a comprehensive Zip file. Alternatively, one can easily <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/rn\/f3-998197.html#f3-1007592 create a dependency report>\r\n% for the current file active in the MATLAB editor\r\n% by using \"Save and Show Dependency Report\" from the Tools menu.\r\n% That approach is much faster than using |depfun| (with its default options), but leaves you then to manually evaluate\r\n% one-by-one each function that your top-level function calls. As you might\r\n% guess, it's easy to miss a necessary file when you create your Zip.\r\n\r\n%%\r\n% Before I started coding, I thought I'd check the File Exchange (wouldn't you?), and I quickly found Malcolm's |exportToZip|.\r\n% Malcolm's file uses his own version of |depfun| (called |mydepfun|), that\r\n% smartly uses non-default behavior of |depfun| to automatically skip files\r\n% in MATLAB Toolboxes; |mydepfun| returns the paths to just those files needed for the target\r\n% Zip file, which is then automatically created. \r\n\r\n%%\r\n% I tried |exportToZip| on |morphTool|; it worked flawlesslyREPLACE_WITH_DASH_DASHand\r\n% quickly! And syntactically, it couldn't be easier to use:\r\n\r\n%%\r\n% |zipfilename = exportToZip(funcname,zipfilename)|\r\n\r\n%%\r\n% Oh, and incidentally, a new version of |morphTool| will go live soon.\r\n% Thanks, MalcolmREPLACE_WITH_DASH_DASHyou saved me a lot of time and effort!\r\n\r\n%%\r\n% You gotta love the File Exchange!\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2465#respond Comments?> \r\n##### SOURCE END ##### dbc53bc2eb94469eb54a9fc2b703639c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Brett's Pick this week is exportToZip, by fellow MathWorker Malcolm Wood.\r\n      \r\n   \r\n   I recently received an email from a File Exchange user informing me that a GUI I had shared... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/18\/easier-and-less-error-prone-creation-of-zip-files\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/2465"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2465"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2465\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}