4

I'm trying to find an error in my code. The error is in a function of 3rd level that initially works perfectly, but somehow at one point stops (the function is called many times within a loop).

The error says the variable is undefined (it doesn't happen at the beginning, but after more than 150 times). Could I write some condition atop the sentence to stop it just before the error? Then I could know why this variable is not defined anymore.

3 Answers 3

9

Use dbstop if error. That dbstop command will take you to command prompt in the stopped function when the error occurs.

You can also get tricky and use the dbstop in FILESPEC at LINENO if EXPRESSION syntax. For example, if you want to break if the variable doesn't exist right before the line that trips the error, say line 224 of myFun.m:

dbstop in myFun.m at 224 if ~exist('x','var')

Then it will stop at line 224 of myFun.m if x is not a variable.

Sign up to request clarification or add additional context in comments.

Comments

5

Type

 dbstop if error

then execute you code.

See doc of dbstop for more options.

2 Comments

Haha, beat you to the punch... by 2 seconds apparently! ;)
It's nightime were I live (Spain) and daytime for you (West USA). I guess I'm already tired and type slow. THAT saved you :-P
0

Check to see if you are out of bounds of the array or other structure you are iterating over

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.