MATLAB: Detect warning and take action

flowundocumentedwarningwarning handling

I have a for loop and I call a function inside it. What I want to do, is detect if this function call created any warnings and if so continue. Something like:
for i=1:len
c = my_func1(arguments);
if (any warning)
continue;
end
end
Any suggestions? I tried to use lastwarn, without success though.

Best Answer

Hello Ioannis,
A common workflow is to use lastwarn in this way:
for ...
warning('') % Clear last warning message
... run code ...
[warnMsg, warnId] = lastwarn;
if ~isempty(warnMsg)
...react to warning...
end
end
But you said lastwarn didn't work for you. How did you use it? What do you mean it didn't work? What kind of warnings were you seeing that weren't being picked up?
-Cam
Related Question