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 end 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; else counter = counter+1; end if counter>=2 flag = true; else flag = false; end
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