how to catch unwanted NaN and Inf

47 views (last 30 days)
John Hopkins
John Hopkins on 6 Jan 2013
Commented: Ghaith Mustafa on 16 Aug 2021
I sometimes unknowingly generate values which are NaN and later on it may cause unexpected problems. I want to catch occurances of NaN or Inf without manually inserting isnan and isinf everywhere. When I use
dbstop if naninf
I have the following problem: dbstop will stop in all sorts of built-in matlab functions. But I only want dbstop to stop when it finds NaN or inf in a variable that is in the scope of the main function I wrote. How do I change this or is there an alternate way to catch unwanted occurances of NaN/inf? Thanks.

Answers (1)

Matt J
Matt J on 7 Jan 2013
Edited: Matt J on 7 Jan 2013
If it's a particular variable you're interested in, you can set a conditional breakpoint using a dbstop syntax like
dbstop in file if any(isnan(A(:))
Or you can set the breakpoint GUI-style in the MATLAB editor as described here http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-234.
  8 Comments
Habib
Habib on 11 May 2013
If the execution stops at a nan, I am able to see what caused that and I can repair it. I have functions calling functions and above all a fzero is utilized inside another fzero... . It is not easy to manually track everything to find possible nan/infs. I face errors by fzero, and it takes quite some time to realize that oh, it origins from a nan in value. You know sometimes returned error messages are missleading.
... I aksed a question yesterday and Matt J, tried my coed and apparently something is wrong with my Matlab installation. It causes dbstop if naninf doesnt work correctly. I have to reinstall or upgrade first.
Ghaith Mustafa
Ghaith Mustafa on 16 Aug 2021
@Image Analyst I have the same problem and i want to set them to zero value. How can i do that

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!