Documentation Center

  • Trials
  • Product Updates

Contents

Quick Start Examples

Add Customized Check to By Product Folder

The following example shows how to add a customized check to a Model Advisor By Product > Demo subfolder. In this example, the customized check does not check model elements.

  1. In your working directory, create the sl_customization.m file, as shown below. This file registers and creates the check registration function defineModelAdvisorChecks, which in turn registers the check callback function SimpleCallback. The function defineModelAdvisorChecks uses a ModelAdvisor.Root object to define the check interface.

    function sl_customization(cm)
    
    % --- register custom checks
    cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
    
    % --- defineModelAdvisorChecks function
    function defineModelAdvisorChecks
    mdladvRoot = ModelAdvisor.Root;
    rec = ModelAdvisor.Check('exampleCheck');
    rec.Title = 'Example of a customized check';
    rec.TitleTips = 'Added customized check to Product Folder';
    rec.setCallbackFcn(@SimpleCallback,'None','StyleOne');
    mdladvRoot.publish(rec, 'Demo'); 
    
    % --- creates SimpleCallback function
    function result = SimpleCallback(system);
    result={};
  2. Close the Model Advisor and your model if either are open.

  3. In the Command Window, enter:

    sl_refresh_customizations
  4. From the MATLAB® window, select New > Model to open a new Simulink® model window.

  5. From the model window, select Analysis > Model Advisor > Model Advisor to open the Model Advisor.

  6. A System Selector — Model Advisor dialog box opens. Click OK. The Model Advisor window opens. It might take a few minutes.

  7. If the By Product folder is not displayed in the Model Advisor window, select Show By Product Folder from the Settings > Preferences dialog box.

  8. In the left pane, expand the By Product folder to display the subfolders.

    The customized check Example of a customized check appears in the By Product > Demo subfolder.

See Also

Create Customized Pass/Fail Check

The following example shows how to create a Model Advisor pass/fail check. In this example, the Model Advisor checks Constant blocks. If a Constant blocks value is numeric, the check fails.

  1. In your working directory, update the sl_customization.m file, as shown below. This file registers and creates the check registration function defineModelAdvisorChecks, which also registers the check callback function SimpleCallback. The function SimpleCallback creates a check that finds Constant blocks that have numeric values. SimpleCallback uses the Model Advisor format template.

    function sl_customization(cm)
    
    % --- register custom checks
    cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
    
    % --- defineModelAdvisorChecks function
    function defineModelAdvisorChecks
    mdladvRoot = ModelAdvisor.Root;
    rec = ModelAdvisor.Check('exampleCheck');
    rec.Title = 'Check Constant block usage';
    rec.TitleTips = ['Fail if Constant block value is a number; Pass if' ...
        ' Constant block value is a letter'];
    rec.setCallbackFcn(@SimpleCallback,'None','StyleOne') 
    
    mdladvRoot.publish(rec, 'Demo');
    
    % --- SimpleCallback function that checks constant blocks
    function result = SimpleCallback(system)
    mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
    result    = {};
    
    all_constant_blk=find_system(system,'LookUnderMasks','all',...
        'FollowLinks','on','BlockType','Constant');
    blk_with_value=find_system(all_constant_blk,'RegExp','On','Value','^[0-9]');
    
    ft = ModelAdvisor.FormatTemplate('ListTemplate');
    ft.setInformation(['This check looks for constant blocks that'...
        'use numeric values']);
    if ~isempty(blk_with_value)
        ft.setSubResultStatusText(['Check has failed. The following '...
            'Constant blocks have numeric values:']);
        ft.setListObj(blk_with_value);
        ft.setSubResultStatus('warn');
        ft.setRecAction('Parameterize the constant block');
        mdladvObj.setCheckResultStatus(false); 
    else
        ft.setSubResultStatusText(['Check has passed. No constant blocks'...
            ' with numeric values were found.']);
        ft.setSubResultStatus('pass');
        mdladvObj.setCheckResultStatus(true); 
    end
    ft.setSubBar(0);
    result{end+1} = ft;
  2. Close the Model Advisor and your model if either are open.

  3. It the Command Window, enter:

    sl_refresh_customizations
  4. From the MATLAB window, select New > Model to open a new Simulink model window.

  5. In the Simulink model window, create two Constant blocks named Const_One and Const_1:

    • Right-click the Const_One block, choose Constant Parameters, and assign a Constant value of one.

    • Right-click the Const_1 block, choose Constant Parameters, and assign a Constant value of 1.

    • Save your model as example2_qs

  6. From the model window, select Analysis > Model Advisor > Model Advisor to open the Model Advisor.

  7. A System Selector — Model Advisor dialog box opens. Click OK. The Model Advisor window opens. It might take a few minutes.

  8. If the By Product folder is not displayed in the Model Advisor window, select Show By Product Folder from the Settings > Preferences dialog box.

  9. In the left pane, click By Product > Demo > Check Constant block usage.

  10. Click Run This Check. The Model Advisor check fails for the Const_1 block and displays a Recommended Action to parametrize the constant block.

  11. Follow the Recommended Action to fix the failed Constant block. In the Model Advisor dialog box:

    • Double-click the example2_qs/Const_1 hyperlink.

    • Change Constant Parameters > Constant value to two, or a nonnumeric value.

    • Rerun the Model Advisor check. Both Constant blocks now pass the check.

See Also

Create Customized Pass/Fail Check with Fix Action

The following example shows how to create a Model Advisor pass/fail check with a fix action. In this example, the Model Advisor checks Constant blocks. If a Constant block value is numeric, the check fails. The Model Advisor is also customized to create a fix action for the failed checks.

  1. In your working directory, update the sl_customization.m file, as shown below. This file contains three functions, each of which use the Model Advisor format template:

    • defineModelAdvisorChecks — Defines the check, creates input parameters, and defines the fix action.

    • simpleCallback — Creates the check that finds Constant blocks with numeric values.

    • simpleActionCallback — Creates the fix for Constant blocks that fail the check.

    function sl_customization(cm)
    
    % --- register custom checks
    cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
    
    % --- defineModelAdvisorChecks function
    function defineModelAdvisorChecks
    mdladvRoot = ModelAdvisor.Root;
    rec = ModelAdvisor.Check('exampleCheck');
    rec.Title = 'Check Constant block usage';
    rec.TitleTips = ['Fail if Constant block value is a number; Pass if '...
        'Constant block value is a letter'];
    rec.setCallbackFcn(@SimpleCallback,'None','StyleOne') 
    
    % --- input parameters 
    rec.setInputParametersLayoutGrid([1 1]);
    inputParam1 = ModelAdvisor.InputParameter;
    inputParam1.Name = 'Text entry example';
    inputParam1.Value='VarNm';
    inputParam1.Type='String';
    inputParam1.Description='sample tooltip';
    inputParam1.setRowSpan([1 1]);
    inputParam1.setColSpan([1 1]);
    rec.setInputParameters({inputParam1});
    
    % -- set fix operation
    myAction = ModelAdvisor.Action;
    myAction.setCallbackFcn(@simpleActionCallback);
    myAction.Name='Fix Constant blocks';
    myAction.Description=['Click the button to update all blocks with'...
        'Text entry example'];
    rec.setAction(myAction);
    
    mdladvRoot.publish(rec, 'Demo'); 
    
    % --- SimpleCallback function that checks constant blocks
    function result = SimpleCallback(system)
    mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
    result    = {};
    
    all_constant_blk=find_system(system,'LookUnderMasks','all',...
        'FollowLinks','on','BlockType','Constant');
    blk_with_value=find_system(all_constant_blk,'RegExp','On','Value','^[0-9]');
    
    ft = ModelAdvisor.FormatTemplate('ListTemplate');
    ft.setInformation(['This check looks for constant blocks that'...
        ' use numeric values']);
    if ~isempty(blk_with_value)
        ft.setSubResultStatusText(['Check has failed. The following '...
            'Constant blocks have numeric values:']);
        ft.setListObj(blk_with_value);
        ft.setSubResultStatus('warn');
        ft.setRecAction('Parameterize the constant block');
        mdladvObj.setCheckResultStatus(false);
        mdladvObj.setActionEnable(true);
    else
        ft.setSubResultStatusText(['Check has passed. No constant blocks'...
            'with numeric values were found.']);
        ft.setSubResultStatus('pass');
        mdladvObj.setCheckResultStatus(true);
    end
    ft.setSubBar(0);
    result{end+1} = ft;
    
    % --- creates SimpleActionCallback function that fixes failed check
    function result = simpleActionCallback(taskobj)
    mdladvObj = taskobj.MAObj;
    result    = {};
    
    system = getfullname(mdladvObj.System);
    
    % Get the string from the input parameter box.
    inputParams = mdladvObj.getInputParameters;
    textEntryEx = inputParams{1}.Value;
    
    all_constant_blk=find_system(system,'LookUnderMasks','all',...
        'FollowLinks','on','BlockType','Constant');
    blk_with_value=find_system(all_constant_blk,'RegExp','On','Value','^[0-9]');
    ft = ModelAdvisor.FormatTemplate('TableTemplate');
    % Define table col titles
    ft.setColTitles({'Block','Old Value','New Value'})
    for inx=1:size(blk_with_value)
       oldVal = get_param(blk_with_value{inx},'Value');
       ft.addRow({blk_with_value{inx},oldVal,textEntryEx});
       set_param(blk_with_value{inx},'Value',textEntryEx);    
    end
    
    ft.setSubBar(0);
    result = ft;
    mdladvObj.setActionEnable(false);
  2. Close the Model Advisor and your model if either are open.

  3. At the command prompt, enter:

    sl_refresh_customizations
  4. From the Command Window, select New > Model to open a new model.

  5. In the Simulink model window, create two Constant blocks named Const_One and Const_1:

    • Right-click the Const_One block, choose Constant Parameters, and assign a Constant value of one.

    • Right-click the Const_1 block, choose Constant Parameters, and assign a Constant value of 1.

    • Save your model as example3_qs.

  6. From the model window, select Analysis > Model Advisor > Model Advisor to open the Model Advisor.

  7. A System Selector — Model Advisor dialog box opens. Click OK. The Model Advisor window opens. It might take a few minutes.

  8. If the By Product folder is not displayed in the Model Advisor window, select Show By Product Folder from the Settings > Preferences dialog box.

  9. In the left pane, click By Product > Demo > Check Constant block usage.

  10. Click Run This Check. The Model Advisor check fails for the Const_1 block. The Model Advisor box has a Fix Constant blocks button in the Action section of the Model Advisor dialog box.

  11. In the Model Advisor Dialog box, enter a nonnumeric value in the Text entry example parameter field in the Analysis section of the Model Advisor dialog box. In this example, the value is VarNm.

  12. Click Fix Constant blocks. The Const_1 Constant block value changes from 1 to the nonnumeric value that you entered in step 10. The Result section of the dialog box lists the Old Value and New Value of the Const_1 block.

  13. In the Model Advisor dialog box, click Run This Check. Both constant blocks now pass the check.

See Also

Was this topic helpful?