Main Content

Suppress Diagnostic Messages Programmatically

The following examples show how to manage diagnostic suppressions programmatically.

Suppress Diagnostic Messages Programmatically

This example shows how to access simulation metadata to manage diagnostic suppressions and to restore diagnostic messages programmatically.

Create a New Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\diagnostic_suppressor_demo.

  2. Change to the docroot\toolbox\simulink\examples folder. At the MATLAB® command line, enter:

    cd(fullfile(docroot, 'toolbox', 'simulink', 'examples')) 

  3. Copy the getDiagnosticObjects.m, suppressor_script.m, and Suppressor_CLI_Demo.slx files to your local working folder.

    The getDiagnosticObjects.m function queries the simulation metadata to access diagnostics that were thrown during simulation. The suppressor_script.m script contains the commands for suppressing and restoring diagnostics to the Suppressor_CLI_Demo model.


Open and Simulate the Model

Open the model. To access Simulink.SimulationMetadata, set the ReturnWorkspaceOutputs parameter value to 'on'. Simulate the model.

model = 'Suppressor_CLI_Demo';
out = sim(model);

Get Message Identifiers from Simulation Metadata

Find the names of diagnostic message identifiers using the simulation metadata stored in the MSLDiagnostic object.

if (exist('out', 'var'))
    diag_objects = getDiagnosticObjects(out);

Several warnings were generated during simulation, including a saturation of the Data Type Conversion block. Query the diag_objects variable to get more information on the identifiers.

ans = 

  MSLDiagnostic with properties:

    identifier: 'SimulinkFixedPoint:util:Saturationoccurred'
       message: 'Saturation occurred. This originated from 'Suppressor_CLI_Demo/Con…'
         paths: {'Suppressor_CLI_Demo/Convert/FixPt To FixPt3'}
         cause: {}
         stack: [0×1 struct]

Suppress Saturation Diagnostic on a Block

Use the Simulink.suppressDiagnostic function to suppress the saturation diagnostic on the data type conversion block only. Simulate the model.

Simulink.suppressDiagnostic('Suppressor_CLI_Demo/Convert/FixPt To FixPt3', ...

Restore the Saturation Diagnostic

Use the Simulink.restoreDiagnostic function to restore the saturation diagnostic of the same block.

Simulink.restoreDiagnostic('Suppressor_CLI_Demo/Convert/FixPt To FixPt3',...

Suppress Multiple Diagnostics on a Source

You can suppress multiple warnings on a single source by creating a cell array of message identifiers. Suppress the precision loss and parameter underflow warnings of the Constant block, one, in the model.

diags = {'SimulinkFixedPoint:util:fxpParameterPrecisionLoss',...

Restore All Diagnostics on a Block

Restore all diagnostics on a specified block using the Simulink.restoreDiagnostic function.


Suppress a Diagnostic on Many Blocks

You can suppress one or more diagnostics on many blocks. For example, use the find_system function to create a cell array of all Data Type Conversion blocks in a system, and suppress all saturation warnings on the specified blocks.

dtc_blocks = find_system('Suppressor_CLI_Demo/Convert',...
 'BlockType', 'DataTypeConversion');
Simulink.suppressDiagnostic(dtc_blocks, 'SimulinkFixedPoint:util:Saturationoccurred');

Restore All Diagnostics Inside a Subsystem

You can also use the Simulink.restoreDiagnostic function to restore all diagnostics inside a specified subsystem.

 'FindAll', 'On');

Add Comments and User Information to a Suppression

A SuppressedDiagnostic object contains information on the source of the suppression and the suppressed diagnostic message identifier. You can also include comments, and the name of the user who last modified the suppression.

Object = Simulink.SuppressedDiagnostic('Suppressor_CLI_Demo/Convert/FixPt To FixPt1',...
Object.Comments = 'Reviewed: John Doe';
Object.LastModifiedBy = 'Joe Schmoe'
Object = 

  SuppressedDiagnostic with properties:

            Source: 'Suppressor_CLI_Demo/Convert/FixPt To FixPt1'
                Id: 'SimulinkFixedPoint:util:Saturationoccurred'
    LastModifiedBy: 'Joe Schmoe'
          Comments: 'Reviewed: John Doe'
      LastModified: '2016-Jun-21 18:23:01'

Get Suppression Data

To get suppression data for a certain subsystem or block, use the Simulink.getSuppressedDiagnostics function.

Object = Simulink.getSuppressedDiagnostics('Suppressor_CLI_Demo/Convert/FixPt To FixPt1');

Restore All Diagnostics on a Model

When a model contains many diagnostic suppressions, and you want to restore all diagnostics to a model, use the Simulink.getSuppressedDiagnostics function to return an array of Simulink.SuppressedDiagnostic objects. Then use the restore method as you iterate through the array.

Objects = Simulink.getSuppressedDiagnostics('Suppressor_CLI_Demo');
for iter = 1:numel(Objects)

Suppress Diagnostic Messages of a Referenced Model

This example shows how to suppress a diagnostic when the diagnostic originates from a referenced model. By accessing the MSLDiagnostic object of the specific instance of the warning, you can suppress the warning only for instances when the referenced model is simulated from the specified top model.

This example model contains two instances of the same referenced model, RefModel. The model RefModel references yet another model, RefModel_Low. RefModel_Low contains two Gain blocks that each produce a wrap on overflow warning during simulation. Suppress one of the four instances of this warning in the model by accessing the MSLDiagnostic object associated with the wrap on overflow warning produced by one of the Gain blocks in the RefModel_Low model only when it is referenced by Ref_block1.

Open the top model. Simulate the model and store the output in a variable, out.

out = sim('TopModel');

Access the simulation metadata stored in the MSLDiagnostic object.

diag = getDiagnosticObjects(out)
diag = 

  1×4 MSLDiagnostic array with properties:


You can view the diagnostics and their causes in the Diagnostic Viewer or at the command-line.

for i = 1 : numel(diag)

Suppress one of the wrap on overflow warnings from RefModel_Low only when it is simulated from TopModel/Ref_block1 by accessing the specific diagnostic. Simulate the model.

out = sim('TopModel')

Access the simulation metadata. This simulation produced only three warnings.

diag = getDiagnosticObjects(out)
diag = 

  1×3 MSLDiagnostic array with properties:


Restore the diagnostic to the model.


See Also

| | | | |