how to catch unwanted NaN and Inf

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.

Risposte (1)

Matt J
Matt J il 7 Gen 2013
Modificato: Matt J il 7 Gen 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 Commenti

Habib
Habib il 10 Mag 2013
Matt, thanks for your answer. what if I have multiple functions and they call each other and I want to stop in any of my files a nan or inf occurs for any variable? without stoping in matlab builtin functions!
Image Analyst
Image Analyst il 10 Mag 2013
Modificato: Image Analyst il 10 Mag 2013
I'd doubt you would stop in any built-in functions unless the input was already bad. Use the isnan() or isinf() functions, wherever you think you need to, to detect any nans or infs, and take action such as correct them or alert the user.
Habib
Habib il 10 Mag 2013
Image Analyst, please take a look at my own question about this subject: http://www.mathworks.com/matlabcentral/answers/75300-how-to-use-dbstop-if-naninf
Image Analyst, please take a look at my own question about this subject: http://www.mathworks.com/matlabcentral/answers/75300-how-to-use-dbstop-if-naninf
and I tried isinf() :
Warning: Breakpoint expression
'isinf()' resulted in an error. The
error was
Error using ==> isinf
Not enough input arguments.
Matt J
Matt J il 10 Mag 2013
Modificato: Matt J il 10 Mag 2013
ISINF works like this:
>> isinf([1,2,3, Inf, 4 5])
ans =
0 0 0 1 0 0
To find NaNs as well, best to use ~isfinite()
>> ~isfinite([NaN,2,3, Inf, 4 -Inf])
ans =
1 0 0 1 0 1
What do you want to do with them once you find (if you find) nans and infs? Set them to some other value? Remove them totally from the vector (shortening the vector)?
Habib
Habib il 11 Mag 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.
@Image Analyst I have the same problem and i want to set them to zero value. How can i do that

Accedi per commentare.

Tag

Richiesto:

il 6 Gen 2013

Commentato:

il 16 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by