通过编程方式从模型顾问检查分析中排除模块
为了节省运行模型顾问检查的时间和阅读结果的精力,您可以限制模型顾问对模型的分析范围。您可以以编程方式创建模型顾问排除项,以从检查中过滤掉某些块或块类型。您可以创建以下排除项:
Simulink® 积木
Stateflow® 图
您可以将排除项保存到您正在运行检查的模型的 SLX 文件中,或者保存到单独的排除项文件中。
此示例展示了基于 UI 的工作流程的程序化替代方案。请参阅从模型顾问检查分析中排除模块。最后,您将创建一个排除项,修改并删除该排除项,并将两组排除项保存到不同的文件中。
过滤器类型
您可以使用过滤类型来定义要从检查中排除的 Simulink 块或 Stateflow 实体的类型。虽然此示例仅使用了两种类型,但您也可以将这些名称-值参量用于带有 filterType 参量的函数,以控制排除实体的类型:
Simulink 过滤器
Block- 排除 Simulink 模块。BlockType- 排除所有特定类型的块。Subsystem- 排除子系统内的所有块。Library- 排除所有 Library 模块实例。MaskType- 排除掩码类型的块或子系统。Stateflow- 排除 Simulink 中的 Stateflow 块。
Stateflow 过滤器
Chart- 排除 Stateflow 图中的所有实体。State- 排除 Stateflow 状态。Transition- 排除 Stateflow 过渡。Junction- 排除 Stateflow 连接点。GraphicalFunction- 排除 Stateflow 连接点。MATLABFunction- 排除 Stateflow MATLAB 函数。SimulinkFunction- 排除 Stateflow Simulink 函数。TruthTable- 排除 Stateflow 真值表。SimulinkBasedState- 排除基于 Stateflow Simulink 的状态。
您不能排除其他实体,例如 MATLAB 功能块中的信号和代码行。
打开示例模型
要打开示例模型,请运行以下命令:
open_system("modelAdvisorExclusions");
该模型使用 MATLAB Function 模块来计算输入 a 和 b 的商和余数。还有两个 MATLAB 函数块,用于检查输出的商和余数是否为整数。
设置检查
本示例使用以下带有检查 ID 的检查:
检查信号标签的位置 —
mathworks.jmaab.db_0097检查 MATLAB Function 的输入和输出设置 —
mathworks.maab.na_0034
运行此代码以设置和运行检查。
checkIds = {'mathworks.maab.na_0034', 'mathworks.maab.db_0142'};
results = ModelAdvisor.run('modelAdvisorExclusions', checkIds);Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.
Running Model Advisor...
Systems passed: 0 of 1
Systems with information: 0 of 1
Systems with warnings: 1 of 1
Systems with failures: 0 of 1
Systems with incomplete run: 0 of 1
Systems with justifications: 0 of 1
Summary Report
模型的结果对应于 results 中的第一个 ModelAdvisor.SystemResult 对象。要打开模型顾问结果的 HTML 报告,请输入:
viewReport(results{1})报告显示,由于某些模块标签位于块上方,并且 MATLAB 函数块没有明确定义的输入和输出数据类型,因此该模型对两项检查都生成了警告。

创建排除项
1.添加对 MATLAB 功能模块类型的排除项。
Advisor.addExclusion('modelAdvisorExclusions', BlockType='MATLAB Function');
2.您可以使用 Advisor.getExclusion 函数来验证此排除项是否存在。您可以使用此命令查看模型中的所有排除项,或者使用可选的过滤参量查看单个排除项。
Advisor.getExclusion('modelAdvisorExclusions', BlockType='MATLAB Function')
ans =
AdvisorFilterSpecification with properties:
filteredItem: [1×1 advisor.filter.IFilter]
metadata: [1×1 advisor.filter.Metadata]
mode: Exclude
commentThread: [1×0 advisor.filter.Metadata Sequence]
type: BlockType
id: 'MATLAB Function'
checks: [1×1 String Sequence]
使用 Advisor.getExclusion 只能查看检查的排除项。除非存在针对特定模块的排除规则,否则您无法看到是否已排除模块。在这种情况下,Advisor.getExclusion 返回 MATLAB 功能模块类型的排除项,但并未指定要排除单个 MATLAB 功能块。
3.删除之前的模型顾问报告,然后运行模型顾问检查。
rmdir(fullfile('slprj','modeladvisor'),'s'); ModelAdvisor.run('modelAdvisorExclusions', checkIds);
Running Model Advisor...
Systems passed: 0 of 1
Systems with information: 0 of 1
Systems with warnings: 1 of 1
Systems with failures: 0 of 1
Systems with incomplete run: 0 of 1
Systems with justifications: 0 of 1
Summary Report
HTML 报告显示,模型顾问在两项检查中均排除了 MATLAB 函数块。此外,由于排除了 MATLAB 函数的输入和输出,因此对 MATLAB 函数的输入和输出的检查也通过了。

移除和修改现有排除项
对 MATLAB 功能块的排除范围太广,导致对 MATLAB 功能输入和输出的检查变得多余。移除此排除项,并仅添加对检查输入信号 Check quotient 和 Check remainder 数据类型的两个 MATLAB 函数块的排除项。此外,这些排除项仅适用于 MATLAB 函数输入和输出的检查,而不适用于标签位置的检查。
1.删除您在上一节中创建的排除项。
Advisor.removeExclusion('modelAdvisorExclusions', BlockType='MATLAB Function');
除非您为排除块的子集定义了特定的排除项,否则您不能使用 Advisor.removeExclusion 函数来删除排除块子集的排除项。
2.仅对检查传入信号数据类型的块添加排除项,这些排除项仅适用于使用可选的 checks 参量对 Advisor.addExclusion 函数输入和输出进行检查的 MATLAB 函数输入和输出。
Advisor.addExclusion('modelAdvisorExclusions', Block='modelAdvisorExclusions/Check quotient', checks={'mathworks.maab.na_0034'}); Advisor.addExclusion('modelAdvisorExclusions', Block='modelAdvisorExclusions/Check remainder', checks={'mathworks.maab.na_0034'});
3.删除之前的报告并运行模型顾问检查。
rmdir(fullfile('slprj','modeladvisor'),'s'); ModelAdvisor.run('modelAdvisorExclusions', checkIds);
Running Model Advisor...
Systems passed: 0 of 1
Systems with information: 0 of 1
Systems with warnings: 1 of 1
Systems with failures: 0 of 1
Systems with incomplete run: 0 of 1
Systems with justifications: 0 of 1
Summary Report
HTML 报告显示,对 MATLAB 函数输入和输出的检查在排除指定块的情况下运行,而对标签位置的检查则在没有任何排除的情况下运行。

保存到文件和从文件加载
您可以从文件中存储和加载排除项,以便您可以快速切换排除项或与他人共享它们。保存两组排除项:一组是您在上一节中定义的,另一组是空的排除项。
1.使用 Advisor.saveExclusion 函数的 filePath 参量保存您在上一节中定义的排除项。
Advisor.saveExclusion('modelAdvisorExclusions', filePath='excl.xml');
2.清除所有已加载的排除项。将此配置保存到模型文件中,方法是将 Advisor.saveExclusion 函数的 filePath 参量设置为 ''。
Advisor.clearExclusion('modelAdvisorExclusions'); Advisor.saveExclusion('modelAdvisorExclusions', filePath='');
3.加载您保存到 excl.xml 的排除项。模型顾问会在您再次运行检查时使用这些排除项。
Advisor.loadExclusion('modelAdvisorExclusions', 'excl.xml'); rmdir(fullfile('slprj','modeladvisor'),'s'); ModelAdvisor.run('modelAdvisorExclusions', checkIds);
Running Model Advisor...
Systems passed: 0 of 1
Systems with information: 0 of 1
Systems with warnings: 1 of 1
Systems with failures: 0 of 1
Systems with incomplete run: 0 of 1
Systems with justifications: 0 of 1
Summary Report
4.加载您保存到模型文件中的空排除集。运行模型顾问检查时,不会排除任何项目。
Advisor.loadExclusion('modelAdvisorExclusions', ''); rmdir(fullfile('slprj','modeladvisor'),'s'); ModelAdvisor.run('modelAdvisorExclusions', checkIds);
Running Model Advisor...
Systems passed: 0 of 1
Systems with information: 0 of 1
Systems with warnings: 1 of 1
Systems with failures: 0 of 1
Systems with incomplete run: 0 of 1
Systems with justifications: 0 of 1
Summary Report
5.删除报告,清理目录。
rmdir(fullfile('slprj','modeladvisor'),'s');
另请参阅
Advisor.addExclusion | Advisor.removeExclusion | Advisor.clearExclusion | Advisor.getExclusion | Advisor.saveExclusion | Advisor.loadExclusion