Breakpoint-counter: Only stop at Matlab breakpoint the second (nth) time it is hit

I am looking for a way to set up a conditional breakpoint that only stops the second (or n-th) time the condition is met, for example:

function f = myfunc(x)
for t = 1:1000
   x = x+x^0.5; %I want to stop here the second time the function is called and t == 666
f = x;

I know how to stop at the breakpoint each time the condition is met, but if I want to look at the second time it is hit I first need to wait for a few minutes, then hit f5 and wait for another few minutes. This is quite annoying as it makes me lose focus once more then required. If I want to look at the 10th time, it is even worse.

I am looking for a solution that does not require me to adjust the code of the function in which I need to stop.


Here's a fun idea, that I think gives you a partial solution.

Write a function mystop.m:

function flag = mystop

persistent counter

if isempty(counter)
    counter = 0;
    counter = counter+1;

if counter>=2
    flag = true;
    flag = false;

Now set a conditional breakpoint within myfunc at the line x = x+x^0.5;, with the condition being

t == 666 && feval(@()mystop)

Set up a test function to exercise myfunc a few times:

function [f1,f2,f3,f4,f5] = mytest

clear mystop

f1= myfunc(1)
f2= myfunc(2)
f3= myfunc(3)
f4= myfunc(4)
f5= myfunc(5)

When you run mytest, it should stop only the second time the line in myfunc is hit (and t is 666) - in other words, while f2 is being calculated.

Note that you need to include the line clear mystop, in order to reset the persistent variable between calls to mytest. Alternatively, you could just manually call clear mystop between runs of mytest.

Obviously - if you want it stop only after the 3rd, 10th, nth time etc, modify the condition counter>=2 above to counter>=n.

You'll HAVE to adjust code, manage a flag there which can count the number of times it have reached there

Need Your Help

Storing Grails/GORM domain objects in the session - why not?

hibernate grails gorm

I'm learning Grails/GORM and as I've understood it the current best practice is not to store domain objects in the session (see for a potential fix).

Where can I find the range interval partition tablespaces in the data dictionary?

oracle partitioning intervals

For a range interval partitioned table, you can specify multiple tablespaces like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.