创建函数以检查多个系统
您可以使用 ModelAdvisor.run 函数以编程方式对一个或多个模型或子系统运行检查。本示例展示了如何创建一个函数,该函数对多个子系统运行模型顾问检查,然后返回故障和警告的数量。
本示例还介绍了如何修改函数以并行检查多个模型或子系统。如果您拥有 Parallel Computing Toolbox™ 许可证,则可以并行运行此函数以减少处理时间。
编写一个函数来运行检查并返回结果
1.在 MATLAB® 窗口中,选择新建 > 函数。
2.将文件另存为 run_configuration.m。
3.在函数中,右键点击 untitled 并选择用文件名替换函数名。函数名称已更新为 run_configuration。
function [outputArg1,outputArg2] = run_configuration(inputArg1,inputArg2) %UNTITLED Summary of this function goes here % Detailed explanation goes here outputArg1 = inputArg1; outputArg2 = inputArg2; end
4.删除函数体。
function [outputArg1,outputArg2] = run_configuration(inputArg1,inputArg2) end
5.将输出参量替换为 [fail,warn],将输入参量替换为 SysList。
function [fail,warn] = run_configuration(SysList) end
6.在函数内部,指定模型顾问配置文件。
fileName = 'myCheckConfiguration.json';
7.SysList 输入是模型顾问要运行检查的系统列表。对 SysList 调用 ModelAdvisor.run 函数。
SysResultObjArray = ModelAdvisor.run(SysList,'Configuration',fileName);
8.确定返回失败和警告的检查数量,并分别将它们输出到 fail 和 warn 输出参量:
fail = 0; warn = 0; for i=1:length(SysResultObjArray) fail = fail + SysResultObjArray{i}.numFail; warn = warn + SysResultObjArray{i}.numWarn; end
run_configuration 函数现在包含以下内容:
function [fail, warn] = run_configuration(SysList) %RUN_CONFIGURATION Check systems with Model Advisor % Check systems given as input and return number of failures and warnings. fileName = 'myCheckConfiguration.json'; % Run the Model Advisor. SysResultObjArray = ModelAdvisor.run(SysList,'Configuration', fileName); fail = 0; warn = 0; for i=1:length(SysResultObjArray) fail = fail + SysResultObjArray{i}.numFail; warn = warn + SysResultObjArray{i}.numWarn; end end
测试函数
1.保存 run_configuration 函数。
2.创建 sl_customization 文件,这是本示例中自定义检查配置所必需的。有关自定义配置文件的更多信息,请参阅 使用模型顾问配置编辑器来自定义模型顾问。
copyfile customizationFile.m sl_customization.m f
3.将要运行模型顾问检查的子系统保存到名为 systems 的变量中。
systems = {'sldemo_auto_climatecontrol/Heater Control',...
'sldemo_auto_climatecontrol/AC Control',...
'sldemo_auto_climatecontrol/Interior Dynamics'};4.对 systems 运行 run_configuration 函数。
[fail,warn] = run_configuration(systems);
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 3
Systems with information: 0 of 3
Systems with warnings: 3 of 3
Systems with failures: 0 of 3
Systems with incomplete run: 0 of 3
Systems with justifications: 0 of 3
Summary Report
4.请使用“汇总报告”或 disp 函数查看结果:
要查看每个系统的模型顾问报告,点击“摘要报告”链接。这将打开模型顾问命令行摘要报告。
要查看
run_configuration函数返回的失败数和警告数,请查看fail和warn变量。
disp(['Number of checks that return failures: ', num2str(fail)]);Number of checks that return failures: 0
disp(['Number of checks that return warnings: ', num2str(warn)]);Number of checks that return warnings: 5
并行检查多个系统
并行检查多个系统可以减少模型顾问所需的处理时间。如果您拥有 Parallel Computing Toolbox™ 许可证,则可以在多核主机上并行检查多个系统。
要并行检查多个系统,请调用 ModelAdvisor.run 函数,并将 'ParallelMode' 设置为 'On'。
SysResultObjArray = ModelAdvisor.run(SysList,'Configuration',fileName,'ParallelMode','On');
Parallel Computing Toolbox 不支持 32 位 Windows® 计算机。
每个并行处理一次对一个模型运行检查。当模型顾问以并行模式运行时,它不支持基础工作区中的模型数据。模型数据必须在模型工作区或数据字典中定义。
另请参阅
ModelAdvisor.run | ModelAdvisor.setDefaultConfiguration