Main Content

Create a Function for Checking Multiple Systems

You can create a function to programmatically run multiple checks on a model. The function returns the number of failures and warnings.

Copy the Configuration File to Your Current Directory

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

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

  2. Enter this command in the MATLAB command prompt. MATLAB copies the file to your writeable directory.

    copyfile(fullfile(matlabroot,'examples','slcheck','main','prepare_cust_chk_code.m'),...'prepare_cust_chk_code.m','f');
    run('prepare_cust_chk_code.m');
  3. In the example files, notice demoConfiguration.json. This file defines the Model Advisor checks that are executed during the analysis. For more information about creating a custom configuration file, see Use the Model Advisor Configuration Editor to Create a Custom Model Advisor Configuration.

Define the Function to Programmatically Run Checks

  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)

  4. Define the output and input arguments. For the output arguments, press Shift-Enter after entering each value to automatically update inlining instances in the function.

    • output_Arg1 as fail

    • output_Arg2 as warn

    • inputArg1, inputArg2 to SysList

    function [fail, warn] = run_configuration(SysList)
    fail = inputArg1;
    warn = inputArg2;
    

  5. Inside the function, specify the Model Advisor configuration file and the systems against which the analysis is ran:

    SysList={SysList={'AdvisorCustomizationExample/Amplifier',...
        'sldemo_auto_climatecontrol/Heater Control','rtwdemo_iec61508'};
    fileName = 'demoConfiguration.json';
    

  6. Call the ModelAdvisor.run function:

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

  7. Determine the number of checks that return warnings and failures:

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

    The function should now look like this:

    function [fail, warn] = run_configuration(SysList)
    
    %RUN_CONFIGURATION Check systems with Model Advisor
    %   Check systems given as input and return number of warnings and
    %   failures.
    
    SysList={SysList={'AdvisorCustomizationExample/Amplifier',...
        'sldemo_auto_climatecontrol/Heater Control','rtwdemo_iec61508'};
    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
    

  8. Save the function.

  9. Test the function. In the MATLAB Command Window, run run_configuration.m .

  10. Review the results. Click the Summary Report link to open the Model Advisor Command-Line Summary report.

See Also

|

Related Topics