Main Content

Create a Function to Check Multiple Systems

You can use the ModelAdvisor.run function to programmatically run checks on one or more models or subsystems. This example shows how to create a function that runs Model Advisor checks on multiple subsystems and then returns the number of failures and warnings.

This example also describes how you can modify the function to check multiple models or subsystems in parallel. If you have the Parallel Computing Toolbox™ license, you can run this function in parallel mode to reduce the processing time.

Copy the Example Configuration File to Your Current Directory

Copy the custom Model Advisor configuration file demoConfiguration.json to a writeable directory.

1. In MATLAB®, set your current folder to a writeable directory.

2. Copy the script prepare_cust_chk_code.m to your current folder and run the script. The script copies the files necessary for this example to your current folder.

copyfile(fullfile(matlabroot,'examples','slcheck','main','prepare_cust_chk_code.m'),...
    'prepare_cust_chk_code.m','f');

run('prepare_cust_chk_code.m');

The example file demoConfiguration.json. is a custom configuration file. You can use custom configuration files to specify which checks to include in the Model Advisor analysis. For more information about custom configuration files, see Use the Model Advisor Configuration Editor to Customize the Model Advisor.

Write a Function to Run Checks and Return Results

1. In the MATLAB window, select New > Function.

2. Save the file as run_configuration.m.

3. In the function, right-click on untitled and select Replace function name by file name. The function name is updated to 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. Delete the body of the function.

function [outputArg1,outputArg2] = run_configuration(inputArg1,inputArg2)
end

5. Replace the output arguments with [fail,warn] and the input argument with SysList.

function [fail,warn] = run_configuration(SysList)
end

6. Inside the function, specify the Model Advisor configuration file.

fileName = 'demoConfiguration.json';

7. The SysList input is a list of systems for the Model Advisor to run checks on. Call the ModelAdvisor.run function on SysList.

SysResultObjArray = ModelAdvisor.run(SysList,'Configuration',fileName);

8. Determine the number of checks that return failures and warnings and output them to the fail and warn output arguments, respectively:

fail = 0;
warn = 0;

for i=1:length(SysResultObjArray)
    fail = fail + SysResultObjArray{i}.numFail;
    warn = warn + SysResultObjArray{i}.numWarn;
end

The run_configuration function now contains this content:

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 = 'demoConfiguration.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

Test the Function

1. Save the run_configuration function.

2. Save the subsystems that you want to run Model Advisor checks on to a variable called systems.

systems = {'sldemo_auto_climatecontrol/Heater Control',...
    'sldemo_auto_climatecontrol/AC Control',...
    'sldemo_auto_climatecontrol/Interior Dynamics'};

3. Run the run_configuration function on systems.

[fail,warn] = run_configuration(systems);
         Running Model Advisor... 
Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.... ... 

         Systems passed: 0 of 3

         Systems with warnings: 3 of 3

         Systems failed: 0 of 3
         To view the summary report, use the 'ModelAdvisor.summaryReport(SystemResultObjArray)' command. SystemResultObjArray is the result of the ModelAdvisor.run command.

4. Review the results by using the Summary Report or the disp function:

  • To view the Model Advisor reports for each system, click the Summary Report link. This opens the Model Advisor Command-Line Summary report.

  • To view the number of failures and warnings returned by the run_configuration function, look at the fail and warn variables.

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: 4

Checking Multiple Systems in Parallel

Checking multiple systems in parallel reduces the processing time required by the Model Advisor. If you have a Parallel Computing Toolbox™ license, you can check multiple systems in parallel on a multicore host machine.

To check multiple systems in parallel, call the ModelAdvisor.run function with 'ParallelMode' set to 'On'.

SysResultObjArray = ModelAdvisor.run(SysList,'Configuration',fileName,'ParallelMode','On');

The Parallel Computing Toolbox does not support 32-bit Windows® machines.

Each parallel process runs checks on one model at a time. When the Model Advisor runs in parallel mode, it does not support model data in the base workspace. Model data must be defined in the model workspace or data dictionary.

See Also

|

Related Topics