Try/Catch Block not operating as predicted. Can anyone help me fix this?!
9 次查看(过去 30 天)
显示 更早的评论
I am using a try catch block to iterate through a directory of images. I have the try/catch blocks nested within a while loop, so the loop will try to load an image, and display a message stating whether it was succesful or not. Either way, it moves onto the next iteration and runs agian. The while loop only ends once there have been the same number of succesful loads as there are files in the directory that match the pattern. The goal is to run through all the images in a directory, even if their numberinng is inconsistent.
The problem I am having, though, is that the "error" message I have set to display in the catch block displays even when the image loads succesfully. So I will end up with two messages on a succesful load something like this: "Img01 succesful." followed by "Img01 not found." I can't find where I have gone wrong. I've got a lot of nesting going on, so I am wondering if I am sut missing something that fresh set of eyes on it will spot.
I want the script to only run the if-else statement if the imgfile loaded succesfully, and obviously I only want the "not found" statement to prinnt if the file is not in the directory.
filen=filen=size(dir(strcat(fold,'/*.tif')),1);
while f<filen
% Advance to next file number to try
t=t+1;
imgfile=strcat(fold, '/', fold, num2str(t,'%02d'), '.tif');
% Try to load file, catch in me object if not found
try
% Load and run some analyses on imgfile
img=imread(imgfile);
SomeAnalysesHere
% Print success message for loaded file to track progress/confirm
% load and increment file-success index (after succesful process).
fprintf(strcat('\n \n \nFile\f', imgfile, ' succesfully processed. \n'))
f=f+1;
% Append data to output matrix
data(f,1)=t; % Create image-matched index in output file
if stuff==0
data(f,2)=0;
elseif stuff==1
data(f,2)=stuff;
data(f,3)=other_stuff;
data(f,4)=avg_stuff;
data(f,5)=size_of_stuff;
else
data(f,2)=stuff;
data(f,3)=other_stuff;
data(f,4)=avg_stuff;
for j=1:stuff
data(f,5+j)=size_of_stuff(j);
end
end
% Print custom message if file not found and increment try-counter
% to try next index for file
catch
fprintf(strcat('\n \n \nFile\f', imgfile, ' was not found. \n Advancing to next file. \n'))
end
end
0 个评论
回答(1 个)
Steven Lord
2021-4-26
You're assuming that the only reason the code inside the try block could error is an inability to find the file. Modify your catch block and check the identifier of the error that caused MATLAB to enter the catch block.
A = 0;
try
A(1, 2) = [1 2]; % Can't put two elements into one element of A
catch ME
fprintf("The code threw error with ID '%s'\nand message '%s'.\n", ...
ME.identifier, ME.message)
end
My guess is that one of your stuff variables is a different size than you think it is.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!