Main Content

Review a Model Against Conditions that You Specify with the Model Advisor

This example demonstrates how to create two simple check types: a pass/fail check with no fix action and an informational check. A basic pass/fail check finds and reports what the check is reviewing and whether the check passes or fails. An informational check finds and displays a description of what the check is reviewing and any references to applicable standards.

Create an sl_customization Function

In your working folder, create the sl_customization.m file. To register the custom checks, within the sl_customization.m file, create an sl_customization(cm) function as shown here. This function accepts one argument, a customization object. This customization manager object includes the addModelAdvisorCheckFcn method for registering the custom checks. The input to this method is a handle to the function (defineModelAdvisorChecks) that contains calls to two check definition functions. These functions contain the definitions of the simple pass/fail check and the informational check.

function sl_customization(cm)
% SL_CUSTOMIZATION - Model Advisor customization demonstration.

% Copyright 2019 The MathWorks, Inc.

% register custom checks 

% -----------------------------
% defines Model Advisor Checks
% -----------------------------
function defineModelAdvisorCheck

Create the Check Definition Function for a Pass/Fail Check with No Fix Action

In this section, you create the check definition function that checks whether a Constant block value is a number or a letter. If the value is a number, the check produces a warning. If the value is a letter, the check passes.

This check uses the DetailStyle type of callback function. This style allows you to view results by block, subsystem, or recommended action. Applying this style produces default formatting, so that you do not have to use the ModelAdvisor.FormatTemplate class or the other Model Advisor formatting APIs to format the results that appear in the Model Advisor. You specify this style as an input to the setCallbackFcn method.

Create a new file, definePassFailCheck.m, and enter the function shown here:

function definePassFailCheck
mdladvRoot = ModelAdvisor.Root;
rec = ModelAdvisor.Check('simplePassFailCheck');
rec.Title = 'Check Constant block usage';
rec.TitleTips = ['Warn if Constant block value is a number; Pass if' ...
    ' Constant block value is a letter'];

mdladvRoot.publish(rec, 'Demo');

% --- Callback function that checks Constant blocks
function simplePassFailCheck(system,CheckObj)
mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
if isempty(violationBlks)
    ElementResults = ModelAdvisor.ResultDetail;
    ElementResults.Description = 'Identify Constant blocks with a value that is a number.';
    ElementResults.Status = 'All Constant blocks have a value that is a letter.';
    for i=1:numel(violationBlks)
	 ElementResults(1,i) = ModelAdvisor.ResultDetail;
    for i=1:numel(ElementResults)
        ElementResults(i).Description = 'Identify Constant blocks with a value that is a number.';
        ElementResults(i).Status = 'The following Constant blocks have values that are numbers:';
        ElementResults(i).RecAction =  'Change the Constant block value to a letter.';
This check identifies Constant block values that are numbers and produces a warning, but it does not provide a fix action. For more information on how to create a check definition function with a fix, see Fix a Model to Comply with Conditions that You Specify with the Model Advisor.

Create the Check Definition Function for an Informational Check

In this section, you create a check definition function for an informational check that finds and displays the model configuration and checksum information.

For an informational check, the Model Advisor displays the overall check status, but the status is not in the result. In addition, an informational check does not include the following items in the results:

  • A description of the status.

  • The recommended action to take when the check does not pass.

  • Subcheck results.

Create a new file, defineInformationCheck.m, and enter the function shown here:

function defineInformationCheck

% Create ModelAdvisor.Check object and set properties.
rec = ModelAdvisor.Check('com.mathworks.sample.infocheck');
rec.Title = 'Identify model configuration and checksum information';
rec.TitleTips = 'Display model configuration and checksum information';

% Publish check into Demo group.
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.publish(rec, 'Demo');


% -----------------------------
% This callback function uses the DetailStyle CallbackStyle type. 
% -----------------------------

function modelVersionChecksumCallbackUsingFT_Detail(system,CheckObj)

model = bdroot(system);
mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
ElementResults = ModelAdvisor.ResultDetail;
ElementResults.Description = 'Display model configuration and checksum information';   

% If running the Model Advisor on a subsystem, add note to description.
if strcmp(system, model) == false          
    ElementResults.Status = 'NOTE: The Model Advisor is reviewing a subsystem, but these results are based on root-level settings.';    
    ElementResults(end + 1) = ModelAdvisor.ResultDetail;

% If error is encountered, use these values.
mdlver = 'Error - could not retrieve Version';
mdlauthor = 'Error - could not retrieve Author';
mdldate = 'Error - could not retrieve Date';
mdlsum = 'Error - could not retrieve CheckSum';

% Get model configuration and checksum information.
    mdlver = get_param(model,'ModelVersion');
    mdlauthor = get_param(model,'LastModifiedBy');
    mdldate = get_param(model,'LastModifiedDate');
    mdlsum = Simulink.BlockDiagram.getChecksum(model);
    mdlsum = [num2str(mdlsum(1)) ' ' num2str(mdlsum(2)) ' ' ...
                     num2str(mdlsum(3)) ' ' num2str(mdlsum(4))];    

lbStr ='<br/>';
resultStr = ['Model Version: ' mdlver lbStr 'Author: ' mdlauthor lbStr ...
        'Date: ' mdldate lbStr 'Model Checksum: ' mdlsum];
ElementResults(end).Status = resultStr;


Run the Custom Checks in the Model Advisor

  1. In the Command Window, enter:

  2. Open the model sldemo_fuelsys by typing this command in the MATLAB command prompt:


  3. In the Modeling tab, select Model Advisor. A System Selector dialog opens. Click OK.

  4. In the left pane, select By Product > Demo > Identify model configuration and checksum information.

  5. Click Run Checks.

    The check passes and displays the information.

  6. In the left pane, select By Product > Demo > Check Constant block usage.

  7. Click Run Checks.

    The check produces a warning because several blocks contain values that are numbers. The results contain links to these blocks. The result displays a Recommended Action.

  8. Follow the Recommended Action to fix the Constant blocks.

See Also

| |

Related Topics