Reputation: 1205
I frequently find myself in the situation, where I insert a lot of keyboard
commands into my code to debug it. However, I would like to have a bit more flexibility. That's why I started writing my stahp
function.
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
The idea is, to let the user choose, what he wants to do (continue, abort, debug, maybe something else in the future). However, I would prefer to not start the debug mode inside the stahp
function, but right after stepping out.
For example, when running
function test_stahp
a = 1
stahp()
b = 2
end
I would like to enter the debug mode right before b=2
. I assume, that dbstep out
can somehow be used, but the way I tried it so far, you still have to step manually out of stahp()
. I am also aware, that the recursive call of stahp()
in otherwise
might complicate things, but I could remove that part.
Any help is very much appreciated. Thank you.
Upvotes: 2
Views: 51
Reputation: 517
You can use dbstack
to get the current stack and then retrieve the name of the calling function and line number that called stahp
. Using this info, you can use the dbstop
to create a breakpoint in the function that performed the function call. The code sample below sets the breakpoint in test_stahp
on the b=2 line.
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
Upvotes: 1