本文介绍了每当被0除或Inf值出现时,停止或暂停运行MatLab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有这么多m文件的计算量很大时,跟踪为什么会出错确实是一项艰巨的任务.

When there are so many m-files with so many calculations, tracking why this and that went wrong is really a big task.

通常只是发现代码中某处被0除,或者为某些变量保存了Inf值.

Often it just turns out that somewhere in the code there was division by 0, or value of Inf saved for some variable.

我想使它易于检查.一种方法是写

I want to make this easy to check. One way is to write

if a == Inf
   display('a is infinite')
   keyboard
end

如果我每次都为每个变量添加这样的内容,它将起作用,但是会很杂乱.

If I add things like this everytime for every variable, it will work but it will be messy.

如果在运行过​​程中Inf有某些变量,我可以使MatLab停止或暂停或弹出警告消息吗?

Can I make MatLab stop or pause or pop up a warning message if some variable is Inf in the process of running?

推荐答案

Matlab有一个不错的调试套件表示许多即时条件.特别是命令

Matlab has a nice debugging suite for a number of on-the-fly conditions.In particular, the command

dbstop if naninf

,它将在找到NaNInf条件后暂停执行一行. 文档上提供了一个示例.使用此功能的唯一潜在缺点是,如果将任何变量初始化为NaNInf(某些代码会这样做(令我感到困惑)),则会发生暂停.

which will pause execution a line after a NaN or Inf condition is found. An example is given on the documentation. The only potential downside of using this feature is that the pause occurs if any variables are initialized as NaN or Inf, which some codes do (to my confusion).

dbstop 确实具有更精确,更强大的暂停执行系统,但是我认为naninf选项与良好的测试相结合是进行一般开发的绝佳起点.

dbstop does have a more pinpoint and potentially robust system of pausing execution, but I think the naninf option combined with good tests is an excellent place to start for general development.

可以通过以下方法从GUI调用相同的行为:转到断点">更多错误和警告处理选项">"NaN或Inf",然后选择正确的单选按钮.

The same behavior can be invoked from the GUI by going to Breakpoints > More Error and Warning Handling Options > NaN or Inf and selecting the correct radio button.

这篇关于每当被0除或Inf值出现时,停止或暂停运行MatLab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:01