Breakpoints in code. Something equivalent to the Stop command in interpreted Basic.

I'm wanting to put permanent breakpoints in code. Something equivalent to the Stop command in interpreted Basic. As with the Editor's breakpoints, I want the cursor to be at the interruption point. And I want to continue with F5. Any ideas?

 Accepted Answer

Use the keyboard function.

3 Comments

Thanks for your note. I use keyboard sometimes, though I don't like it. I have to track down the interruption point
If you have the Editor/Debugger preference "Automatically open files when MATLAB reaches a breakpoint" enabled (in the "General Preferences for the Editor/Debugger" section of this documentation page) it will automatically open the file in which the keyboard command is called to the line where it is called.
Steven, Thanks for your response. Yes, "Automatically open files when MATLAB reaches a breakpoint" is turned on. Perhaps it wasn't before. I installed ML 2016b this afternoon, and perhaps that default (?) is different than what I had before. I am seeing the curser at the 'keyboard' statement, and perhaps wasn't before. So using 'keyboard' is perhaps the best I can do in ML.
Thanks, also, to Walter.

Sign in to comment.

More Answers (1)

8 Comments

Thanks for your answer. I've tried several applications of dbstop, and they all seem cumbersome. I wish I could just fix something equivalent to the editor's breakpoints into code.
The editor breakpoints are dbstop commands. If you click in the editor to set a breakpoint and use "dbstatus" you will see a dbstop is in effect; likewise if you dbstop and then open the file in the editor you will see the breakpoint graphic in the appropriate place.
To stop at the line after the current one, you can use
ST = dbstack; dbstop('in', ST.file, 'at', str2num(ST.line+1));
Walter, thanks for your response.
Your solution looks like magic to me.
ST = dbstack; dbstop('in', ST.file, 'at', str2num(ST.line+1));
Is the syntax correct?
I pasted your text into a script "ProjOpt2.m" called by "ProjOpt1.m".
When ML hit the line, it came back with (in red): Error using + Too many input arguments.
Error in ProjOpt2 (line 10) ST = dbstack; dbstop('in', ST.file, 'at', str2num(ST.line+1));
Error in ProjOpt1 (line 772) ProjOpt2 % chain the next big code block
Ah, it should probably be
ST = dbstack; dbstop('in', ST(1).file, 'at', str2num(ST(1).line+1));
function STOP
ST = dbstack;
if length(ST) < 2; return; end
dbstop('in', ST(2).file, 'at', str2num(ST(2).line+1));
end
Put that on your path, and then you should be able to insert calls to
STOP
Note: I have not tested to see what happens if the next line is not executable or is the end of a control structure.
Thanks Walter, very useful little function! You have there a little typo though. It should read num2str and not str2num.
Thank you, gentlemen. I'll try the STOP function when I'm next using Matlab.
Walter's STOP worked perfectly for me but only once I changed str2num to num2str, since dbstop receives a string as argument and ST(2).line is double. It works even when it is called before a comment or blank line (it stops in the next executable line, if it exists). Thank you, Walter!
EDIT. From dbstop help: To resume execution, use dbcont or dbstep. To exit from the debugger, use dbquit.

Sign in to comment.

Categories

Products

Asked:

on 6 Mar 2017

Edited:

on 8 Jul 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!