Reading images from a loop

4 views (last 30 days)
Jekaterina
Jekaterina on 3 May 2015
Commented: Jekaterina on 3 May 2015
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

Accepted Answer

Image Analyst
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
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.
Jekaterina
Jekaterina on 3 May 2015
Agree cell arrays complicate things, but I do need them later on. Many thanks for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!