{"id":430,"date":"2011-12-23T07:00:31","date_gmt":"2011-12-23T12:00:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=430"},"modified":"2019-10-29T17:04:36","modified_gmt":"2019-10-29T21:04:36","slug":"batch-processing-files-in-another-folder","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/23\/batch-processing-files-in-another-folder\/","title":{"rendered":"Batch processing files in another folder"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Blog reader Asadullah posted the following question last week on my old post about <a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/06\/batch-processing\/\">batch processing<\/a>:\r\n   <\/p>\r\n   <p><i>I am trying to process some images by following the MATLAB demo. After getting the names of files when I try to see any of\r\n         the files then it gives the error. The detail is as follows:<\/i><\/p><pre> &gt;&gt; fileFolder = fullfile(matlabroot,'work','original');\r\n &gt;&gt; dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.jpg'));\r\n &gt;&gt; fileNames = {dirOutput.name}'<\/pre><pre> fileNames =<\/pre><pre>     'AT3_1m4_001.jpg'\r\n     'AT3_1m4_002.jpg'\r\n     'AT3_1m4_003.jpg'\r\n     'AT3_1m4_004.jpg'\r\n     'AT3_1m4_005.jpg'\r\n     'AT3_1m4_006.jpg'\r\n     'AT3_1m4_007.jpg'<\/pre><pre>     [...]<\/pre><pre>     'AT3_1m4_116.jpg'\r\n     'AT3_1m4_117.jpg'\r\n     'AT3_1m4_118.jpg'\r\n     'AT3_1m4_119.jpg'\r\n     'AT3_1m4_120.jpg'<\/pre><pre> &gt;&gt; I=imread(fileNames{1});\r\n ??? Error using ==&gt; imread\r\n File \"AT3_1m4_001.jpg\" does not exist.<\/pre><p><i>I could not understand where is the problem? Please help me to solve this.<\/i><\/p>\r\n   <p>Several people have asked me this same question, so I thought I should post the answer so everyone can see it instead of replying\r\n      in a comment.\r\n   <\/p>\r\n   <p>First, though, I want to strongly recommend against placing your own work files (data, code, etc.) underneath the MATLAB program\r\n      folder, as the Asadullah's code shows in this line:\r\n   <\/p><pre>   fileFolder = fullfile(matlabroot,'work','original');<\/pre><p>The function <tt>matlabroot<\/tt> returns the location of the MATLAB program folder. On a typical Windows computer, for example, this location might be something\r\n      like:\r\n   <\/p><pre>   C:\\Program Files\\MATLAB\\R2011b<\/pre><p>Windows doesn't consider this to be an area where a user will store their own files, and most backup programs will not normally\r\n      back up files that are stored here. Instead, store your own work elsewhere. On Windows, a good choice is somewhere under \"My\r\n      Documents\".\r\n   <\/p>\r\n   <p>OK, now back to the question at hand. The problem is that the string being passed to <tt>imread<\/tt> contains only the filename, such as 'AT3_1m4_116.jpg', and not the full folder location. That's not enough information for\r\n      <tt>imread<\/tt> to be able to find the file.\r\n   <\/p>\r\n   <p>You have to add the full folder location to the image filename before passing it to <tt>imread<\/tt>. In Asadullah's example, the corrected code would look something like this:\r\n   <\/p><pre>   I = imread(fullfile(fileFolder,fileNames{1}));<\/pre><p>Hope that helps! May your days be filled with happily processing large numbers of images.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_548a2ec3f09f49ed99b5217ee89c3bdd() {\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='548a2ec3f09f49ed99b5217ee89c3bdd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 548a2ec3f09f49ed99b5217ee89c3bdd';\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 = 'Steve Eddins';\r\n        copyright = 'Copyright 2011 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_548a2ec3f09f49ed99b5217ee89c3bdd()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\n548a2ec3f09f49ed99b5217ee89c3bdd ##### SOURCE BEGIN #####\r\n%%\r\n% Blog reader Asadullah posted the following question last week on my old\r\n% post about <https:\/\/blogs.mathworks.com\/steve\/2006\/06\/06\/batch-processing\/\r\n% batch processing>:\r\n%\r\n% _I am trying to process some images by following the MATLAB demo. After\r\n% getting the names of files when I try to see any of the files then it\r\n% gives the error. The detail is as follows:_\r\n%\r\n%   >> fileFolder = fullfile(matlabroot,'work','original');\r\n%   >> dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.jpg'));\r\n%   >> fileNames = {dirOutput.name}'\r\n%   \r\n%   fileNames = \r\n%   \r\n%       'AT3_1m4_001.jpg'\r\n%       'AT3_1m4_002.jpg'\r\n%       'AT3_1m4_003.jpg'\r\n%       'AT3_1m4_004.jpg'\r\n%       'AT3_1m4_005.jpg'\r\n%       'AT3_1m4_006.jpg'\r\n%       'AT3_1m4_007.jpg'\r\n%   \r\n%       [...]\r\n%   \r\n%       'AT3_1m4_116.jpg'\r\n%       'AT3_1m4_117.jpg'\r\n%       'AT3_1m4_118.jpg'\r\n%       'AT3_1m4_119.jpg'\r\n%       'AT3_1m4_120.jpg'\r\n%   \r\n%   >> I=imread(fileNames{1});\r\n%   ??? Error using ==> imread\r\n%   File \"AT3_1m4_001.jpg\" does not exist.\r\n%   \r\n%\r\n% _I could not understand where is the problem? Please help me to solve\r\n% this._\r\n%\r\n% Several people have asked me this same question, so I thought I should\r\n% post the answer so everyone can see it instead of replying in a comment.\r\n%\r\n% First, though, I want to strongly recommend against placing your own work\r\n% files (data, code, etc.) underneath the MATLAB program folder, as\r\n% the Asadullah's code shows in this line:\r\n%\r\n%     fileFolder = fullfile(matlabroot,'work','original');\r\n%     \r\n%\r\n% The function |matlabroot| returns the location of the MATLAB program\r\n% folder. On a typical Windows computer, for example, this location might\r\n% be something like:\r\n%\r\n%     C:\\Program Files\\MATLAB\\R2011b\r\n%     \r\n%\r\n% Windows doesn't consider this to be an area where a user will store their\r\n% own files, and most backup programs will not normally back up files that\r\n% are stored here. Instead, store your own work elsewhere. On Windows, a\r\n% good choice is somewhere under \"My Documents\".\r\n%\r\n% OK, now back to the question at hand. The problem is that the string\r\n% being passed to |imread| contains only the filename, such as\r\n% 'AT3_1m4_116.jpg', and not the full folder location. That's not enough\r\n% information for |imread| to be able to find the file.\r\n%\r\n% You have to add the full folder location to the image filename before\r\n% passing it to |imread|. In Asadullah's example, the corrected code would\r\n% look something like this:\r\n%\r\n%     I = imread(fullfile(fileFolder,fileNames{1}));\r\n%     \r\n%\r\n% Hope that helps! May your days be filled with happily processing large\r\n% numbers of images.\r\n\r\n##### SOURCE END ##### 548a2ec3f09f49ed99b5217ee89c3bdd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Blog reader Asadullah posted the following question last week on my old post about batch processing:\r\n   \r\n   I am trying to process some images by following the MATLAB demo. After getting the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/23\/batch-processing-files-in-another-folder\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[154,160,76,498],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/430"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":433,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/430\/revisions\/433"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}