开发者

How can I catch all warnings during a Simulink simulation?

开发者 https://www.devze.com 2023-04-13 06:23 出处:网络
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.

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.

开发者_Python百科

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.


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).


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');


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号