如何在 Simulink 仿真过程中捕获所有警告?

发布于 2024-12-10 07:25:40 字数 491 浏览 0 评论 0原文

我想捕获 Matlab/Simulink 模型仿真期间发出的所有警告。模拟的结果应该是一系列警告,因为可能有多个警告。

理想的代码看起来像这样(除了 catch 不适用于警告):

try
   sim('myModel');
catch warnings
   for i=1:length(warnings)
      <process each warning>
   end
end

我已经尝试过但没有成功的事情:

  • 将警告变成异常不会有帮助,因为我只会收到第一个警告,而不是所有警告他们。
  • 使用我自己的“@char\warnings.m”覆盖内置警告函数只会捕获我自己的脚本中的警告,但不会捕获 sim 函数中的警告。
  • lastwarn 只会给我最后一条警告消息,而不是全部。

PS:我在 Windows 上使用 Matlab 2010b。

I want to catch all warnings issued during the simulation of a Matlab/Simulink model. The result of simulation should be an array of warnings, as there might be more than one warning.

The ideal code would look something like this (except that catch does not work with warnings):

try
   sim('myModel');
catch warnings
   for i=1:length(warnings)
      <process each warning>
   end
end

Things I've tried already without success:

  • Turning the warnings into exceptions won't help, as I will only get the first warning and not all of them.
  • Overriding the built-in warning function with my own "@char\warnings.m" will only catch the warnings in my own script but not in the sim-function.
  • lastwarn will give me only the last warning message, not all of them.

P.S.: I'm using Matlab 2010b on Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相权↑美人 2024-12-17 07:25:40

解决方法是用 diary 记录 sim 的输出,并在模拟后分析文件(警告的格式非常规则)。

A workaround can be to record the output of sim with diary and analyze the file after the simulation (the format of warnings is quite regular).

过气美图社 2024-12-17 07:25:40

不幸的是,我认为这是不可能的。它还提出了一个问题“您想对这些警告做什么?”和“为什么?”。

我会在 Mathworks 技术支持下提出这个问题。 try ... catch ... 严格用于错误,因此我建议使用如下所示的语法:

recordWarnings on
sim('myModel');
warnings = recordWarnings('history');

I don't think that this is possible unfortunately. It also raises the question "what you want to do with the warnings?" and "why?".

I would raise it with Mathworks technical support. The try ... catch ... is strictly for errors, so I would suggest a syntax that looks more like the following:

recordWarnings on
sim('myModel');
warnings = recordWarnings('history');
野鹿林 2024-12-17 07:25:40

如前所述,不可能直接捕获警告,只能捕获模拟命令的输出。但是,当使用 diary 时,输出会写入一个文件,然后必须再次打开、解析和删除该文件。

或者,您可以使用 evalc 命令,该命令直接以字符数组形式返回输出。这应该更容易在脚本中使用。

As already stated, it is not possible to catch the warnings directly, only the output of the simulation command. But when using diary for that, the output is written to a file, which then has to be opened, parsed and deleted again.

Alternatively you can use the evalc command, which directly returns the output as character array. This should be easier to use in scripts.

那一片橙海, 2024-12-17 07:25:40

此解决方案不会向您提供所有警告(为此我建议使用diary),但您可以通过以下方式查看代码的哪些部分生成警告。

myWarnLog = {}
part1
myWarnLog(end+1) = lastwarning
part2
myWarnLog(end+1) = lastwarning
...

我不确定警告是什么样的,但如果需要,您还可以存储有关警告发生时间/地点的信息。
这应该可以让您更有效地进行调试,而无需解析日记

This solution will not give you all warnings (for that I would recommend using the diary), but here is how you can see which parts of your code generate a warning.

myWarnLog = {}
part1
myWarnLog(end+1) = lastwarning
part2
myWarnLog(end+1) = lastwarning
...

I am not sure what warnings look like, but if required you can also store the information about when/where the warning happened.
This should allow you for more efficient debugging without having to parse the diary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文