Uigetdir error when reading images from different directory

3 views (last 30 days)
I have written the below program to display 1000 images in sequence from a directory. Since I am storing the images in different folders I used the uigetdir command at the beginning of my program to select the folders from which the stored images are to be read. However, I get a "File img_00001.pgm does not exist" error when I run the code. The code without the uigetdir works fine without the uigetdir command but the code has to reside in the same folder as that of the image. Below is my code, kindly suggest what might be the mistake that I am committing:
% Read images image_00001.pgm through image_00999.pgm.
uigetdir
for k = 1:1000
% Create an image filename, and read it in to a variable called imageData.
if k<1001 %for images 0 to 9
pgmFileName = sprintf('img_%05d.pgm',k)
if exist(pgmFileName, 'file')
imageData = imread(pgmFileName);
imshow(imageData);
fname = sprintf('FrameNo.%d', k);
title(fname);
pause(0.1);
else
fprintf('File %s does not exist.\n', pgmFileName);
end
plot(imageData);
end
end

Answers (2)

Geoff Hayes
Geoff Hayes on 28 Aug 2014
Abhishek - while your code calls the function uigetdir, you don't make use of what is returned by it. At uigetdir doc, this function returns a string. You need to capture this string and use it with your file names. Something like
folderName = uigetdir;
% if the user presses cancel, then folderName is zero, so only
% continue if it is a string
if ischar(folderName)
for k = 1:1000
% create the filename with its path (folderName)
pgmFileName = fullfile(folderName, sprintf('img_%05d.pgm',k));
if exist(pgmFileName, 'file')
% etc.
end
end
end
Note that I removed your if k<1001 statement as it is unnecessary since your for loop iterates from 1 to 1000, so k will always be less than 1001.
Try the above and see what happens!
  2 Comments
Abhishek
Abhishek on 29 Aug 2014
Dear Goeff,
Thank you for your answer and your interest in my problem. I tried your method but am still getting the same error ("File img_00000.pgm does not exist" and so on). I checked the value stored in the variable folderName and it is storing the correct folderName which I select. Can you please point what I might be doing wrong?
Below is the code which again runs fine without the uigetdir command:
folderName = uigetdir;
% if the user presses cancel, then folderName is zero, so only
% continue if it is a string
if ischar(folderName)
%for displaying images img_00000.pgm to img_00999.pgm
for k = 0:1000
if k<1000
pgmFileName = sprintf('img_%05d.pgm',k);
if exist(pgmFileName, 'file')
% Create an image filename, and read it in to a variable called imageData.
imageData = imread(pgmFileName);
imshow(imageData);
fname = sprintf('FrameNo.%d', k);
title(fname);
pause(0.1);
else
fprintf('File %s does not exist.\n', pgmFileName);
end
plot(imageData);
end
end
end
Geoff Hayes
Geoff Hayes on 29 Aug 2014
Abhishek - you did not add the line of code that "concatenates" the folder name with the file name. Just replace
pgmFileName = sprintf('img_%05d.pgm',k);
with
pgmFileName = fullfile(folderName, sprintf('img_%05d.pgm',k));
Note that you do not need the if k<1000 condition. If you only want to evaluate the code (i.e. read the files) for k less than 1000, then just change the for loop to
for k=0:999

Sign in to comment.


Image Analyst
Image Analyst on 29 Aug 2014
Why not just use the second option in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F which uses dir() to process only those image files that are actually there ?

Categories

Find more on Images 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!