我经常发现自己在代码中插入很多keyboard
命令来调试它。但是,我希望有更多的灵活性。这就是我开始编写stahp
函数的原因。
function stahp()
disp('Stahp requested.');
dbstack
disp('You can either');
disp(' 1 abort.');
disp(' 2 continue.');
disp(' 3 debug.');
in = input('Choose what to do:n');
switch(in)
case 1
error('Stahp.');
case 2
disp('Continue.')
case 3
disp('Debug.');
keyboard % <------------------------------------ Here is my problem
otherwise
stahp();
end
end
这个想法是,让用户选择他想做什么(继续、中止、调试,也许将来会做其他事情)。但是,我不希望在stahp
函数内启动调试模式,而是在退出后立即启动。例如,运行时
function test_stahp
a = 1
stahp()
b = 2
end
我想在b=2
之前进入调试模式。我认为dbstep out
可以以某种方式使用,但按照我迄今为止尝试的方式,您仍然需要手动退出stahp()
。我也知道,otherwise
中对stahp()
的递归调用可能会使事情复杂化,但我可以删除这一部分。
非常感谢您的帮助。非常感谢。
您可以使用dbstack
获取当前堆栈,然后检索调用函数的名称和调用stahp
的行号。使用此信息,可以使用dbstop
在执行函数调用的函数中创建断点。下面的代码示例在b=2行的test_stahp
中设置断点。
function stahp()
disp('Stahp requested.');
dbstack
disp('You can either');
disp(' 1 abort.');
disp(' 2 continue.');
disp(' 3 debug.');
in = input('Choose what to do:n');
r=dbstack;
switch(in)
case 1
dbclear(r(2).name,num2str(r(2).line+1));
error('Stahp.');
case 2
dbclear(r(2).name,num2str(r(2).line+1));
disp('Continue.')
case 3
disp('Debug.');
dbstop(r(2).name,num2str(r(2).line+1))
otherwise
stahp();
end
end