Try/Catch Block not operating as predicted. Can anyone help me fix this?!

10 ビュー (過去 30 日間)
Jesse Ivers
Jesse Ivers 2021 年 4 月 26 日
コメント済み: Jesse Ivers 2021 年 4 月 26 日
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

回答 (1 件)

Steven Lord
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
The code threw error with ID 'MATLAB:subsassigndimmismatch' and message 'Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.'.
My guess is that one of your stuff variables is a different size than you think it is.
  1 件のコメント
Jesse Ivers
Jesse Ivers 2021 年 4 月 26 日
Ahh...I think you're right. I think I traced it all the way back to the funciton stuff was coming from and my input into the stuff matrix was conflicting with how I was later extracting it. Thanks for the help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by