主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

自定义诊断消息

诊断查看器显示 Simulink® 模型运行时操作期间生成的诊断消息。您可以使用回调、S-Function 或 MATLAB Function 模块中的 MATLAB® 错误函数来自定义错误消息。

显示自定义文本

此示例显示如何使用 MATLAB Function 模块自定义错误消息。

  1. 考虑一个具有 Constant、MATLAB Function 和 Display 模块的模型。

    Simulink model using a MATLAB Function block.

  2. MATLAB Function 模块编辑器中,创建一个名为 check_signal 的函数,当 MATLAB Function 模块的输入为负数时,显示自定义消息 Signal is negative

    function y = check_signal(x)
    	if x < 0 
    		error('Signal is negative');
    	else
    		y = x;
    	end
    
  3. 对模型进行仿真。

  4. MATLAB Function 模块的输入为负数时,诊断查看器将显示自定义错误消息。

Diagnostic Viewer displaying the custom error message.

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

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

错误消息示例超链接
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 Function 模块的函数 check_signal 以显示指向 find_system 函数文档的超链接。

  1. 考虑一个具有 Constant、MATLAB Function 和 Display 模块的模型。

    Simulink model using a MATLAB Function block.

  2. MATLAB Function 模块编辑器中,创建一个函数 check_signal,当 MATLAB Function 模块的输入为负数时,显示指向 find_system 函数文档的超链接。

    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. MATLAB Function 模块的输入为负数时,诊断查看器会显示带有超链接的自定义消息。

    Diagnostic Viewer window displaying the custom error message with hyperlink.

另请参阅

工具

主题