Clear Filters
Clear Filters

Skipping iteration on child throwing a warning

1 view (last 30 days)
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 Comments
Image Analyst
Image Analyst on 28 Sep 2023
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 on 28 Sep 2023
@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.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 28 Sep 2023
Edited: Bruno Luong on 28 Sep 2023
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

More Answers (1)

Image Analyst
Image Analyst on 28 Sep 2023
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 Comment
Bruno Luong
Bruno Luong on 28 Sep 2023
Edited: Bruno Luong on 28 Sep 2023
warning cannot be branched by try/catch, only error can.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!