Skipping iteration on child throwing a warning
1 次查看(过去 30 天)
显示 更早的评论
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
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
2023-9-28
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
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
0 个评论
更多回答(1 个)
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
2023-9-28
编辑:Bruno Luong
2023-9-28
warning cannot be branched by try/catch, only error can.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!