Main Content

Suppress Diagnostic Messages Programmatically

The following examples show how to manage diagnostic suppressions programmatically.

Note

Not all diagnostic IDs can be suppressed. Only IDs that have the Suppress button in the Diagnostic Viewer or the Suppress hyperlink in the MATLAB® Command Window can be suppressed.

Suppress Diagnostic Messages Programmatically

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

Open and Simulate the Model

The local example folder contains the getDiagnosticObjects.m, suppressor_script.m, and the Suppressor_CLI_Demo.slx model. 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 the model. To access Simulink.SimulationMetadata class, 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 Conversion block. Query the diag_objects variable to get more information on the identifiers.

  diag_objects(5)

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 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');
  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');

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 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)
      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.

Model with two instances of referenced model RefModel.

RefModel_Low model block

Diagnostic Viewer window displaying the wrap on overflow warnings.

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

| | | | |