【问题标题】:MATLAB debugging: smarter way to stop the code with an specific condition?MATLAB 调试:在特定条件下停止代码的更智能方法?
【发布时间】:2015-12-01 18:27:39
【问题描述】:

当我调试较长的数字代码时,我经常希望在发生某些事情时或在特定的迭代中查看函数变量值。通常我会这样做:

function banana(platano)

% long stuff here

for ii=1:123456789
     % tons of maths
   if ii==45612
      stophere=1;    % I put a break point in this line of code
   end
end

但是,这需要我在函数中编写代码进行调试,而且看起来不太好。有没有更聪明的方法来做到这一点?

【问题讨论】:

    标签: matlab debugging


    【解决方案1】:

    其中一种方法是使用Conditional Breakpoints。您可以通过右键单击行号并选择"Set conditional Breakpoints..." 选项来添加它们。

    例子:

    如本答案的 cmets 中所述,如果您想使用命令行进行设置,可以使用

    dbstop in filename at linenumber if condition 
    

    举个例子:

    dbstop in banana at 6 if ii==454345433
    

    请注意,at linenumber 和 if condition 是可选的。

    更多内容

    调试器的另一个有用工具是在出现错误时中断程序,使用dbstop if error,如显示in this Q&A。

    感谢@Dev-il 给我看这个!

    【讨论】:

    • 我根本不知道。我总是手动放入if 语句并以这种方式暂停代码。顺便说一句,如果您可以通过命令提示符通过命令行而不是使用编辑器来展示如何执行此操作,那就更好了。这可能吗?
    • @rayryeng dbstop in file if condition
    • @rayryeng dbstop in banana at 8 if ii==454345433
    • Ander,考虑添加如何通过命令行执行此操作以使您的帖子完整。我们中的一些人(比如我)不使用 MATLAB 的编辑器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 2016-05-21
    • 2019-12-17
    • 2016-11-14
    • 1970-01-01
    相关资源
    最近更新 更多