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.

     getDiagnosticObjects.m

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';
open_system(model);
set_param(model,'ReturnWorkspaceOutputs','on');
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);
end

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

diag_objects(5)
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', ...
'SimulinkFixedPoint:util:Saturationoccurred');
set_param(model,'SimulationCommand','start');

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',...
 'SimulinkFixedPoint:util:Saturationoccurred');
set_param(model,'SimulationCommand','start');

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',...
 'SimulinkFixedPoint:util:fxpParameterUnderflow'};
Simulink.suppressDiagnostic('Suppressor_CLI_Demo/one',diags);
set_param(model,'SimulationCommand','start');

Restore All Diagnostics on a Block

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

Simulink.restoreDiagnostic('Suppressor_CLI_Demo/one');
set_param(model,'SimulationCommand','start');

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 Converter 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');
set_param(model,'SimulationCommand','start');

Restore All Diagnostics Inside a Subsystem

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

Simulink.restoreDiagnostic('Suppressor_CLI_Demo/Convert',...
 'FindAll', 'On');
set_param(model,'SimulationCommand','start');

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',...
 'SimulinkFixedPoint:util:Saturationoccurred');
Object.Comments = 'Reviewed: John Doe';
Object.LastModifiedBy = 'Joe Schmoe'
set_param(model,'SimulationCommand','start');
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');
set_param(model,'SimulationCommand','start');

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 Simulink.SuppressedDiagnostic objects. Then use the Simulink.SuppressedDiagnostic.restore method as you iterate through the array.

Objects = Simulink.getSuppressedDiagnostics('Suppressor_CLI_Demo');
for iter = 1:numel(Objects)
    restore(Objects(iter));
end
set_param(model,'SimulationCommand','start');

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:

    identifier
    message
    paths
    cause
    stack

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

for i = 1 : numel(diag)
    disp(diag(i));
    disp(diag(i).cause{1});
end

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.

Simulink.suppressDiagnostic(diag(1));
out = sim('TopModel')

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

diag = getDiagnosticObjects(out)
diag = 

  1×3 MSLDiagnostic array with properties:

    identifier
    message
    paths
    cause
    stack

Restore the diagnostic to the model.

Simulink.restoreDiagnostic(diag(1));

See Also

| | | | |