我正在寻找一种设置条件断点的方法,该断点仅停止满足条件的第二(或第n)时间,例如:
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;
我知道每次遇到条件时如何在断点处停止,但是如果我想第二次看到它,我首先需要等待几分钟,然后按f5再等几分钟。这非常令人讨厌,因为它让我再次失去焦点。如果我想第10次看,那就更糟了。
我正在寻找一种不需要我调整我需要停止的功能代码的解决方案。
答案 0 :(得分:2)
这是一个有趣的想法,我认为可以为您提供部分解决方案。
写一个函数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
现在在myfunc
行x = x+x^0.5;
内设置条件断点,条件为
t == 666 && feval(@()mystop)
设置测试功能以练习myfunc
几次:
function [f1,f2,f3,f4,f5] = mytest
clear mystop
f1= myfunc(1)
f2= myfunc(2)
f3= myfunc(3)
f4= myfunc(4)
f5= myfunc(5)
当您运行mytest
时,它应该只停止第二时间myfunc
中的行被击中(t为666) - 换句话说,f2
1}}正在计算中。
请注意,您需要包含行clear mystop
,以便在调用mytest
之间重置持久变量。或者,您可以在clear mystop
的运行之间手动调用mytest
。
显然 - 如果您希望它仅在第3次,第10次,第n次等之后停止,请将上面的条件counter>=2
修改为counter>=n
。
答案 1 :(得分:1)
你必须调整代码,在那里管理一个可以计算它到达那里的次数的标志