matlab中inf值可以换成0吗_每当被0除或Inf值出现时,停止或暂停运行MatLab

bd96500e110b49cbb3cd949968f18be7.png

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

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.

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

解决方案

Matlab has a nice debugging suite for a number of on-the-fly conditions.

In particular, the command1

dbstop if 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 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.

1 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值