How to debug only the N-th call to a function?

3 views (last 30 days)
I have a script that calls fmincon to minimize a function, call it @myfun. The solver makes it through 3 iterations (each with dozens of function evaluations of @myfun), and then it seems to be forever stuck on the 4th iteration. Every time I use ctrl+c to exit the function terminates during a particular subfunction of @myfun, so I suspect something is going on there (i.e. the subfunction is taking forever to evaluate with a particular input that the solver is giving it). In order to investigate this I could put a breakpoint in the subfunction, but then I would have to press F5 about 1000 times before getting to the point where the problem is. What is the best way to debug something like this (where the breakpoint needs to be inside of a function that is called many times, but you don't want to stop until the N-th evaluation)?
Thanks!

Accepted Answer

the cyclist
the cyclist on 10 Sep 2015
Edited: the cyclist on 10 Sep 2015
One possible solution is to set a condition breakpoint. You can read more here: http://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html.
You might need to add some kind of global counter, given your situation.
  1 Comment
Joseph Cheng
Joseph Cheng on 10 Sep 2015
I learned something completely new today! thanks cyclist. Before knowing this I personally stuck snippits of debug code like:
%%%%%debug
if interestingVariable==somenumber
disp('debug');
end
%%%%debug end
%line you're interested here
or since it is a subfunction display the output of a critical computation instead of debug.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!