Filter löschen
Filter löschen

How to exit a loop if file not found

5 Ansichten (letzte 30 Tage)
farheen asdf
farheen asdf am 10 Nov. 2015
Kommentiert: farheen asdf am 10 Nov. 2015
Hi. I'm trying to read several images from a folder. However, the image names are not continuous. For example image 2 is followed by image 4 followed by image 12. I can not change the names of the images. I want the program to exit the loop once an image is not found (if image 3 is not found, it starts the next iteration and reads image 4). How do i do that? This is my code.
clear all;
for AT = 1:4
fdir=fullfile('D:\Documents\Research\MAM LUBNA\Images\BRATS-1 JPG');
filename=sprintf('BRATS_HG000%d_T2.jpg',AT);
I1 = imread(fullfile(fdir,filename));
Im{AT} = I1(:,:,1); % im1=rgb2gray(c1);
GLCM{AT} = graycomatrix(Im{AT},'Offset',[2 0;0 2]);
end

Antworten (1)

Thorsten
Thorsten am 10 Nov. 2015
Bearbeitet: Thorsten am 10 Nov. 2015
Use "exist" to check if the file exists:
fdir=fullfile('D:\Documents\Research\MAM LUBNA\Images\BRATS-1 JPG');
for AT = 1:4
filename=fullfile(fdir,sprintf('BRATS_HG000%d_T2.jpg',AT));
if exist(filename, 'file')
I1 = imread(filename));
Im{AT} = I1(:,:,1); % im1=rgb2gray(c1);
GLCM{AT} = graycomatrix(Im{AT},'Offset',[2 0;0 2]);
end
end
  1 Kommentar
farheen asdf
farheen asdf am 10 Nov. 2015
I tried using the code below but then it always skips the commands below.
if exist(filename) == 0
continue;
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by