如何找到 simulink 模型使用的 matlab 脚本中哪个断言失败?

How do I find which assertion failed in matlab scripts used by simulink model?

我从 simulink 模型调用一些 matlab 脚本,这些脚本使用 assert()。当一个断言失败时,simulink 给了我一个完全无用的断言,没有关于断言发生在哪个子系统或脚本的任何细节,更不用说行号了:

An error occurred while running the simulation and the simulation was terminated
Caused by:
An error occurred during simulation of Model block '<blah>/Model'.
Assertion failed.

然而,这只是一个模型块,它包含许多子系统和脚本块等等。

关于如何找到触发了我的许多断言中的哪一个的任何提示?

不确定是否重要,但所有这些脚本都使用 %#codegen 标签。

assert() 支持自定义错误信息:

assert(cond,msg) throws an error and displays the error message, msg, if cond is false.

assert(cond,msg,A1,...,An) displays an error message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function, if cond is false. Each conversion character in msg is converted to one of the values A1,...,An.

assert(cond,msgID,msg) throws an error, displays the error message, msg, and includes an error identifier on the exception, if cond is false. The identifier enables you to distinguish errors and to control what happens when MATLAB encounters the errors.

assert(cond,msgID,msg,A1,...,An) includes an error identifier on the exception and displays a formatted error message.

由于您可以访问 运行 中的脚本,您可以更新它们以包含详细的错误消息。

例如:

>> assert((2+2) == 5)
Assertion failed.

对比

>> assert((2+2) == 5, 'The rules of The Universe still hold')
Some rules of The Universe still hold