Main Content

自定义诊断消息

诊断查看器显示仿真过程中执行的 MATLAB® 误差函数的输出。

您可以按照以下方式,通过在回调、S-Function 或 MATLAB Function 模块中使用 MATLAB 误差函数来自定义仿真错误消息。

显示自定义文本

此示例说明如何自定义 MATLAB 函数 check_signal 以显示文本 Signal is negative

  1. 打开 MATLAB Function 以进行编辑。

    Simulink model using a MATLAB Function block.

  2. 在 MATLAB 编辑器中,创建一个函数以显示自定义文本。

    function y = check_signal(x)
    	if x < 0 
    		error('Signal is negative');
    	else
    		y = x;
    	end
    

  3. 对模型进行仿真。

    此时将显示运行时错误,并提示您启动调试器。点击确定

  4. 要在诊断查看器中查看以下错误,请关闭调试器。

Diagnostic Viewer displaying this error message: "An error occurred while running the simulation and the simulation was terminated caused by: Signal is negative."

创建文件、文件夹或模块的超链接

要在错误消息中创建文件、文件夹或模块的超链接,请将它们的路径包含在自定义文本中。

错误消息示例超链接
error ('Error reading data from "C:/Work/designData.mat"')在 MATLAB 编辑器中打开 designData.data
error ('Could not find data in folder "C:/Work"')打开命令行窗口,将 C:\Work 设置为工作文件夹。
error ('Error evaluating parameter in block "myModel/Mu"')显示模型 myModel 中的模块 Mu

创建编程超链接

此示例说明如何自定义 MATLAB 函数 check_signal,以显示指向 find_system 文档的超链接。

  1. 打开 MATLAB Function 以进行编辑。

    Simulink model using a MATLAB Function block.

  2. 在 MATLAB 编辑器中,创建一个函数以显示自定义文本。

    function y = check_signal(x)
    	if x < 0
    		error('See help for <a href="matlab:doc find_system">find_system</a>');
    	else
    		y = x;
    	end
    

  3. 对模型进行仿真。

    此时将显示运行时错误,并提示您启动调试器。点击确定

  4. 要在诊断查看器中查看以下错误,请关闭调试器。

Diagnostic Viewer window displaying this error message, "An error occurred while running the simulation and the simulation was terminated caused by: See help for find_system"

相关主题