Reading images from a loop
4 views (last 30 days)
Show older comments
I am trying to read images from a folder with a loop, but the's constantly this error:
Error using imread (line 350)
File "C:\Users\Katya\Documents\MATLAB\sunset\uniform\IMG_6806.JPG.jpg" does not exist.
Error in Finale (line 13)
LogoSet(i).sample = imread(s);
There's a code snippet:
srcFiles = dir('C:\Users\Katya\Documents\MATLAB\sunset\uniform\*.jpg');
% folder with source images
counter = num2str(length(srcFiles));
template = struct('sample', []);
LogoSet = repmat(template, counter);
for i = 1 : numel(LogoSet)
s = ['C:\Users\Katya\Documents\MATLAB\sunset\uniform\' srcFiles(i).name '.jpg'];
LogoSet(i).sample = imread(s);
LogoSet(i).sample = rgb2gray(LogoSet(i).sample);
end
Image does exist in a folder but apparently MATLAB does not recognizes it. It executes correctly the file name but still says that it's not there. Tried with defining a folder - also didn't work. Any help will be appreciated
0 Comments
Accepted Answer
Image Analyst
on 3 May 2015
After you get srcFiles, put this code and tell me what happens when you run it.
folder = 'C:\Users\Katya\Documents\MATLAB\sunset\uniform';
filePattern = fullfile(folder, '*.jdpg');
srcFiles = dir(filePattern)
numFiles = length(srcFiles)
if numFiles == 0
message = sprintf('There are no jpg files are in folder:\n%s', folder);
uiwait(warndlg(message));
else
fprintf('There are %d files in %s:\n', numFiles, folder);
for k = 1 : numFiles
fprintf(' %s\n', srcFiles(k).name);
end
end
Does it tell you that there are no files there, or does it list the filenames in the command window?
3 Comments
Image Analyst
on 3 May 2015
Why are you messing around with cell arrays???? Don't do that - there's no need for that level of complication, unless you need the list of filenames after the loop exists for some reason. Simply use the filename string. I think you should look at the second code chunk in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
If you do need to save SampleNames after the loop ends, then you need to add an index onto SampleNames:
SampleNames(i) = {srcFiles(i).name};
Now you'll be using the i'th cell. Before you were trying to use the ith cell but it was not a cell ARRAY, just a single cell, so you can't index it. It was just one cell, so you can't get the 2nd, 3rd, 4th, etc. cell from it because those aren't there. There is only one cell unless you tell it to create additional cells by indexing it.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!