MATLAB debugging: smarter way to stop the code with an specific condition?

前端 未结 1 1695
庸人自扰
庸人自扰 2020-12-06 11:37

When I debug long pieces of numerical code, often I want to see the function variable values if something happens, or in an specific iteration. Generally I do:



        
1条回答
  •  有刺的猬
    2020-12-06 11:47

    One of the ways is using Conditional Breakpoints. You can add them by right clicking on the number of the line and selecting the "Set conditional Breakpoints..." option.

    Example:

    As described in the comments of this answer, if you want to set it with the command line you can use

    dbstop in filename at linenumber if condition 
    

    As an example:

    dbstop in banana at 6 if ii==454345433
    

    note, that the at linenumber and if condition are optional.

    More things

    Another useful tool of the debugger is breaking the program if there has been an error, using dbstop if error, as show in this Q&A.

    Thanks to @Dev-il for showing me this!

    0 讨论(0)
提交回复
热议问题