Skipping iteration on child throwing a warning

조회 수: 1 (최근 30일)
Aboltabol
Aboltabol 2023년 9월 28일
댓글: Bruno Luong 2023년 9월 28일
I have a script X that has:
for x = 1:5000
output{x} = Script_Y(input(:,:,:,x));
end
Script_Y has a particular loop (among other code):
for alpha = 1:(size(input, 3)-1)
output_preprocessed = Script_Z(input(:,:,alpha), input(:,:,alpha+1));
end
% Later, some more operations are committed on output_preprocessed to give
% the function output.
Script_Z has a loop (among other code):
% Some operations are committed on function inputs to get input_postprocessed_1 and input_postprocessed_2
for k = 1:200
x = input_postprocessed_1/input_postprocessed_2
% More steps to make x precise %
end
% Later, some more operations are committed on x to give
% the function output.
It might occur that for some k, input_postprocessed_1 is singular or near-singular and as soon as Matlab throws this warning, I want to skip the particular iteration in the master loop at my script X! For that x, output{x} shall return NULL or nothing - either sufices for me.
How to go about?
  댓글 수: 2
Image Analyst
Image Analyst 2023년 9월 28일
What exactly is the warning? Please paste back here all the orange or red text that you see when it produces the warning.
Bruno Luong
Bruno Luong 2023년 9월 28일
@Image Analyst OP wrote " input_postprocessed_1 is singular or near-singular "
Actually it input_postprocessed_2 at the right division and the warning is likely the infamous
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = XXXXXX.

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2023년 9월 28일
편집: Bruno Luong 2023년 9월 28일
Modification of script_Z
for k = 1:200
lastwarn('',''); % clean the warning state
x = input_postprocessed_1/input_postprocessed_2;
[~,warnid] = lastwarn;
if strcmp(warnid, 'MATLAB:singularMatrix');
x = []; % I assume this is output_preprocessed returned to script Y
break
end
% More steps to make x precise %
end
script_Y
for alpha = 1:(size(input, 3)-1)
output_preprocessed = Script_Z(input(:,:,alpha), input(:,:,alpha+1));
if isempty(output_preprocessed)
break
end
end
script_X
for x = 1:5000
output{x} = Script_Y(input(:,:,:,x));
% output{x} is empty if the division fails ( input_postprocessed_2is
% singular)
end

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 9월 28일
You can use try catch:
for k = 1 : 200
try
% In the try block put the code that might throw an error.
catch ME
% If you get here an error occurred.
continue; % To continue with next iteration, or use "break" to exit the loop.
end
end
  댓글 수: 1
Bruno Luong
Bruno Luong 2023년 9월 28일
편집: Bruno Luong 2023년 9월 28일
warning cannot be branched by try/catch, only error can.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by