Matlab debug技巧

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/laoxuan2011/article/details/52750305

Matlab debug技巧

说明

实用而精简的matlab debug技巧教程.

Matlab调试头号利器

在命令窗口输入

dbstop if error

如果运行出现错误,matlab会自动停在出错的那行,并且保存所有相关变量。就是这么方便!

Matlab调试m文件中的函数

dbstop in file

比如,我们有 buggy.m文件如下:

function z = buggy(x)
n = length(x);
z = (1:n)./x;

在命令窗口输入

dbstop in buggy
buggy(1:5)

MATLAB 将显示程序暂停的地方 ,进入 enters debug mode.

2   n = length(x);
K>> 
Type dbquit to exit debug mode.

dbstop in file at location if expression

比如有myprogram.m, 如下:

x = ones(1,10);

for n = 1:10
x(n) = x(n) + 1;
end

设置一个断点在 n >= 4时,然后再运行程序:

dbstop in myprogram at 4 if n>=4
myprogram

这时有:

4   x(n) = x(n) + 1;
K>> 
Type dbquit to exit debug mode.

dbstop in file at location

此外还有dbstop in file at location,这命令实际可以直接用鼠标设定断点就好。

调试必会快捷键

进入debug mode之后必须要学会的几个快捷键:
【F10】:单步运行。每单击一次,程序运行一次,但不进入函数。
【F11】:单步运行。遇到函数时进入函数内,仍单步运行。
【F11+shift】:停止单步运行。如果是在函数中,跳出函数;如果不在函数中,直接运行到下一个断点处。

调试完成,清除断点

清除所有M文件中的所有断点
在命令窗口输入

dbclear all

清除文件名为mfile的文件中的所有断点
在命令窗口输入

dbclear all in mfile:

猜你喜欢

转载自blog.csdn.net/laoxuan2011/article/details/52750305