Reading images from a loop

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

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

Jeketerina's reply moved here since it's not an answer to the original question
That is the output:
srcFiles = 7x1 struct array with fields:
name
date
bytes
isdir
datenum
numFiles = 7
There are 7 files in C:\Users\Katya\Documents\MATLAB\sunset\uniform:
IMG_6800.JPG
IMG_6801.JPG
IMG_6802.JPG
IMG_6803.JPG
IMG_6804.JPG
IMG_6805.JPG
IMG_6806.JPG
I've been working to improve it, but I managed to get further only by running loop two times. Although the size for loop is taken correctly - and the set of images seems to be also of the correct structure, the loop runs only two times.
srcFiles = dir('C:\Users\Katya\Documents\MATLAB\sunset\uniform\*.jpg'); % the folder in which images exists
disp(['Apimtis = ' num2str(length(srcFiles))]);
counter = length(srcFiles);
counter = counter
template = struct('sample', []);
LogoSet = repmat(template, [1 counter]);
set = LogoSet
for i = 1 : numel(LogoSet)
i
SampleNames = {srcFiles(i).name};
LogoSet(i).sample = imread(SampleNames{i});
LogoSet(i).sample = rgb2gray(LogoSet(i).sample);
end
It displays error:
Index exceeds matrix dimensions.
Error in Finale (line 32)
LogoSet(i).sample = imread(SampleNames{i});
Could it be the problem with image set structuring? I see no reasons for it to run only twice.
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.
Agree cell arrays complicate things, but I do need them later on. Many thanks for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image 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!