How to catch warnings?
210 次查看(过去 30 天)
显示 更早的评论
采纳的回答
更多回答(1 个)
Geoff May
2020-7-25
You could use the lastwarn function to reset, then check the last warning. Granted this might not work if the warning you are interested in gets overwritten by a subsequent warning that you would rather discard.
% reset the lastwarn message and id
lastwarn('', '');
% call the function that might throw a warning
diceyFunction();
% now if a warning was raised, warnMsg and warnId will not be empty.
[warnMsg, warnId] = lastwarn();
% you can check the warning message or id, or just throw the warning as an error if desired
if(isempty(warnId))
noProblem();
else
error(warnMsg, warnId);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Error Handling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!