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?

 採用された回答

Steven Lord
Steven Lord 2017 年 3 月 6 日

0 投票

Use the keyboard function.

3 件のコメント

John
John 2017 年 3 月 6 日
Thanks for your note. I use keyboard sometimes, though I don't like it. I have to track down the interruption point
Steven Lord
Steven Lord 2017 年 3 月 6 日
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.
John
John 2017 年 3 月 7 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 3 月 6 日
編集済み: KSSV 2021 年 2 月 10 日

2 投票

8 件のコメント

John
John 2017 年 3 月 6 日
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));
John
John 2017 年 3 月 7 日
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.
Ali Komai
Ali Komai 2020 年 11 月 24 日
Thanks Walter, very useful little function! You have there a little typo though. It should read num2str and not str2num.
John
John 2020 年 11 月 24 日
Thank you, gentlemen. I'll try the STOP function when I'm next using Matlab.
Ivan Nascimento
Ivan Nascimento 2021 年 7 月 8 日
編集済み: Ivan Nascimento 2021 年 7 月 8 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

質問済み:

2017 年 3 月 6 日

編集済み:

2021 年 7 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by