Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging MATLAB: Break before an error at certain line

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.

like image 511
user2768062 Avatar asked Dec 11 '22 10:12

user2768062


2 Answers

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.

like image 59
chappjc Avatar answered Jan 18 '23 05:01

chappjc


Type

 dbstop if error

then execute you code.

See doc of dbstop for more options.

like image 34
Luis Mendo Avatar answered Jan 18 '23 03:01

Luis Mendo