MATLAB: Is there a way to throw an error whenever NaN occurs

error messagenan

In most of my work, NaN is not a possible value. However NaN will not stop Matlab from working, so the error will occur some where after NaN happened. This is a trouble for me because the error message only shows the location where that NaN caused an error, but what I need is to find out where NaN was created.
What I wish is, NaN is set to be a kind of error, or throw an error whenever NaN occurs.
Is there anything that I can do?
Thanks

Best Answer

No, you cannot directly make MATLAB throw an error when a NaN is created. But you can do something close to what you wish. One of the types of "Advanced Error Breakpoint Configuration" that you can set will cause MATLAB to stop execution when an Inf or NaN occurs. Search the documentation page to which I linked for that subsection title for more information.
Related Question