Main Content

Programmatically Exclude Blocks from Model Advisor Check Analysis

To save time spent running Model Advisor Checks and effort in reading the results, you can limit the scope of the Model Advisor analysis of your model. You can programmatically create Model Advisor exclusions to filter out certain blocks or types of blocks from checks. You can create exclusions for:

  • Simulink® blocks

  • Stateflow® charts

You can save exclusions to either the SLX file of the model that you are running checks on or a separate exclusion file.

This example shows a programmatic alternative to the UI-based workflow. See Exclude Blocks from Model Advisor Check Analysis. By the end, you will create an exclusion, modify and remove that exclusion, and save two sets of exclusions to separate files.

Filter Types

You can use filter types to define the types of Simulink blocks or Stateflow entities to exclude from checks. Though this example uses only two types, you can also use these name-value arguments for functions with the filterType argument to control the type of excluded entity:

Simulink Filters

  • Block — Exclude a Simulink block.

  • BlockType —Exclude all blocks of a type.

  • Subsystem — Exclude all blocks inside a Subsystem.

  • Library — Exclude all instances of a Library block.

  • MaskType — Exclude blocks or subsystem of mask type.

  • Stateflow — Exclude Stateflow blocks in Simulink.

Stateflow Filters

  • Chart — Exclude every entity inside a Stateflow Chart.

  • State — Exclude a Stateflow State.

  • Transition — Exclude a Stateflow Transition.

  • Junction — Exclude a Stateflow Junction.

  • GraphicalFunction — Exclude a Stateflow Junction.

  • MATLABFunction — Exclude a Stateflow MATLAB Function.

  • SimulinkFunction — Exclude a Stateflow Simulink Function.

  • TruthTable — Exclude a Stateflow Truth Table.

  • SimulinkBasedState — Exclude a Stateflow Simulink-based state.

You cannot exclude other entities, such as signals and lines of code in MATLAB Function blocks.

Open Example Model

To open the example model, run this command:

open_system("modelAdvisorExclusions");

This model uses a MATLAB Function block to calculate the quotient and remainder of the inputs a and b. There are also two MATLAB Function blocks that check whether the outputted quotient and remainder are integers.

Set Up Checks

This example uses the following checks with check IDs:

Run this code to set up and run the checks.

checkIds = {'mathworks.maab.na_0034', 'mathworks.maab.db_0142'};
results = ModelAdvisor.run('modelAdvisorExclusions', checkIds);
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 1

         Systems with information: 0 of 1

         Systems with warnings: 1 of 1

         Systems with failures or incomplete run: 0 of 1

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

The results for the model correspond to the first ModelAdvisor.SystemResult object in results. To open an HTML report for the Model Advisor results, enter:

viewReport(results{1})

The report shows that the model generates warnings for both checks since some block labels are above the blocks and the MATLAB Function blocks do not have explicitly defined input and output data types.

Create Exclusion

1. Add an exclusion for the MATLAB Function block type.

Advisor.addExclusion('modelAdvisorExclusions', BlockType='MATLAB Function');

2. You can use the Advisor.getExclusion function to verify that this exclusion exists. You can use this command to either view all the exclusions in a model, or view an individual exclusion using an optional filter argument.

Advisor.getExclusion('modelAdvisorExclusions', BlockType='MATLAB Function')
ans = 
  AdvisorFilterSpecification with properties:
     filteredItem: [1x1 advisor.filter.IFilter]
         metadata: [1x1 advisor.filter.Metadata]
             mode: Exclude
    commentThread: [1x0 advisor.filter.Metadata Sequence]
             type: BlockType
               id: 'MATLAB Function'
           checks: [1x1 String Sequence]

Using Advisor.getExclusion only allows you to view the exclusions for a check. You cannot see if you are excluding a specific block unless there is a block exclusion for it. In this case, Advisor.getExclusion returns an exclusion for the MATLAB Function block type, but does not specify that you are excluding individual MATLAB Function blocks.

3. Delete the previous Model Advisor report, then run the Model Advisor checks.

rmdir(fullfile('slprj','modeladvisor'),'s');
ModelAdvisor.run('modelAdvisorExclusions', checkIds);
         Running Model Advisor... 

         Systems passed: 0 of 1

         Systems with information: 0 of 1

         Systems with warnings: 1 of 1

         Systems with failures or incomplete run: 0 of 1

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

The HTML report shows that Model Advisor excluded the MATLAB Function blocks from both checks. In addition, the check for MATLAB Function inputs and outputs passes as a result of this exclusion.

Remove and Modify Existing Exclusions

The exclusion for MATLAB Function blocks is too broad, as it makes the check for MATLAB Function inputs and outputs redundant. Remove this exclusion and add exclusions only for the two MATLAB Function blocks that check the data type of the incoming signals Check quotient and Check remainder. In addition, these exclusions only apply to the check for MATLAB Function inputs and outputs and not the check for label positions.

1. Remove the exclusion you created in the previous section.

Advisor.removeExclusion('modelAdvisorExclusions', BlockType='MATLAB Function');

You cannot use the Advisor.removeExclusion function to remove exclusions for a subset of the excluded blocks unless you define a specific exclusion for the subset of blocks.

2. Add exclusions only for the blocks that check the data type of incoming signals that apply only to the check for MATLAB Function inputs and outputs using the optional checks argument for Advisor.addExclusion.

Advisor.addExclusion('modelAdvisorExclusions', Block='modelAdvisorExclusions/Check quotient', checks={'mathworks.maab.na_0034'});
Advisor.addExclusion('modelAdvisorExclusions', Block='modelAdvisorExclusions/Check remainder', checks={'mathworks.maab.na_0034'});

3. Delete the previous report and run the Model Advisor checks.

rmdir(fullfile('slprj','modeladvisor'),'s');
ModelAdvisor.run('modelAdvisorExclusions', checkIds);
         Running Model Advisor... 

         Systems passed: 0 of 1

         Systems with information: 0 of 1

         Systems with warnings: 1 of 1

         Systems with failures or incomplete run: 0 of 1

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

The HTML report shows that the check for MATLAB Function inputs and outputs runs while excluding the specified blocks and the check for label positions runs without any exclusions.

Save to and Load From File

You can store and load exclusions from files so that you can switch between exclusions quickly or share them with others. Save two sets of exclusions: the one you defined in the previous section and an empty set of exclusions.

1. Save the exclusions that you defined in the previous section by using the filePath argument for the Advisor.saveExclusion function.

Advisor.saveExclusion('modelAdvisorExclusions', filePath='excl.xml');

2. Clear all currently loaded exclusions. Save this configuration to the model file by setting the filePath argument for the Advisor.saveExclusion function to ''.

Advisor.clearExclusion('modelAdvisorExclusions');
Advisor.saveExclusion('modelAdvisorExclusions', filePath='');

3. Load the exclusions that you saved to excl.xml. Model Advisor uses these exclusions when you run the checks again.

Advisor.loadExclusion('modelAdvisorExclusions', 'excl.xml');
rmdir(fullfile('slprj','modeladvisor'),'s');
ModelAdvisor.run('modelAdvisorExclusions', checkIds);
         Running Model Advisor... 

         Systems passed: 0 of 1

         Systems with information: 0 of 1

         Systems with warnings: 1 of 1

         Systems with failures or incomplete run: 0 of 1

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

4. Load the empty exclusion set that you saved to the model file. When you run the Model Advisor checks, they run with no exclusions.

Advisor.loadExclusion('modelAdvisorExclusions', '');
rmdir(fullfile('slprj','modeladvisor'),'s');
ModelAdvisor.run('modelAdvisorExclusions', checkIds);
         Running Model Advisor... 

         Systems passed: 0 of 1

         Systems with information: 0 of 1

         Systems with warnings: 1 of 1

         Systems with failures or incomplete run: 0 of 1

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

5. Clean up the directory by deleting the report.

rmdir(fullfile('slprj','modeladvisor'),'s');

See Also

| | | | |

Related Topics