Batch processing files in another folder
Blog reader Asadullah posted the following question last week on my old post about batch processing:
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 the files then it gives the error. The detail is as follows:
>> fileFolder = fullfile(matlabroot,'work','original'); >> dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.jpg')); >> fileNames = {dirOutput.name}'
fileNames =
'AT3_1m4_001.jpg' 'AT3_1m4_002.jpg' 'AT3_1m4_003.jpg' 'AT3_1m4_004.jpg' 'AT3_1m4_005.jpg' 'AT3_1m4_006.jpg' 'AT3_1m4_007.jpg'
[...]
'AT3_1m4_116.jpg' 'AT3_1m4_117.jpg' 'AT3_1m4_118.jpg' 'AT3_1m4_119.jpg' 'AT3_1m4_120.jpg'
>> I=imread(fileNames{1}); ??? Error using ==> imread File "AT3_1m4_001.jpg" does not exist.
I could not understand where is the problem? Please help me to solve this.
Several people have asked me this same question, so I thought I should post the answer so everyone can see it instead of replying in a comment.
First, though, I want to strongly recommend against placing your own work files (data, code, etc.) underneath the MATLAB program folder, as the Asadullah's code shows in this line:
fileFolder = fullfile(matlabroot,'work','original');
The function matlabroot returns the location of the MATLAB program folder. On a typical Windows computer, for example, this location might be something like:
C:\Program Files\MATLAB\R2011b
Windows doesn't consider this to be an area where a user will store their own files, and most backup programs will not normally back up files that are stored here. Instead, store your own work elsewhere. On Windows, a good choice is somewhere under "My Documents".
OK, now back to the question at hand. The problem is that the string being passed to imread contains only the filename, such as 'AT3_1m4_116.jpg', and not the full folder location. That's not enough information for imread to be able to find the file.
You have to add the full folder location to the image filename before passing it to imread. In Asadullah's example, the corrected code would look something like this:
I = imread(fullfile(fileFolder,fileNames{1}));
Hope that helps! May your days be filled with happily processing large numbers of images.
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。