Main Content

Compare Diagnostic Messages Between Model Simulations

This example shows you how to use the Comparator available in the Diagnostic Viewer to easily identify the new diagnostic messages that a model generates during various run-time operations. It also shows how you can save the diagnostic messages from a specific operation for comparison with a later operation in another MATLAB® session.

When you simulate or build a model, you may get several diagnostic messages. After you modify the model, simulating it again can add and remove some diagnostic messages. You can easily spot these differences in the diagnostic messages by using the Comparator and analyze how different conditions or parameters affect the model.

The Comparator enables you to:

  • Compare the diagnostic messages between any two run-time operations. The operations must be of similar type. For example, a simulation operation must be compared with another simulation operation.

  • View the count of the diagnostic messages that have been added and removed.

  • View newly added diagnostic messages.

  • Copy the comparison report to the clipboard in text and JSON formats.

Use Comparator to Compare Diagnostic Messages

1. Open the model DiagnosticDemo.

model = "DiagnosticDemo";
open_system(model);

2. Open the Diagnostic Viewer. In the Debug tab of Simulink® Toolstrip, select Diagnostics > Diagnostic Viewer.

3. Simulate the model.

4. Add a few sample warnings to the model by setting the StartFcn model callback. In the Simulink Toolstrip, on the Modeling tab, in the Design gallery, click Property Inspector. In the Callbacks section of the Properties tab, select StartFcn and enter this code:

for i = 1:50
sldiagviewer.reportWarning("Sample warning.");
end

Optionally, to create the model callback programmatically, use these commands in the MATLAB Command Window:

setWarningCmd = sprintf([
  'for i = 1:50\n', ...
      'sldiagviewer.reportWarning("Sample warning.");\n', ...
  'end']);
set_param(model,'StartFcn',setWarningCmd);

5. Simulate the model again. You can see the sample warning messages in the Viewer available in the Diagnostic Viewer.

6. To compare the diagnostic messages between the two simulations:

a. Select Comparator from the drop-down menu on the left side of the Diagnostic Viewer.

b. Select the initial simulation stage from the drop-down menu to the right of Compare and the most recent simulation stage from the drop-down menu to the right of with. When comparing diagnostic messages, you must select stages of the same type and in the sequence of occurrence, such as compare an earlier stage with a later stage.

The Comparator shows that 50 new warnings are added and none are removed. It also displays the newly added diagnostic messages.

7. Add another warning message for the next simulation. To add a warning, delete the Scope block from the model and simulate the model. When you simulate the model, the Viewer displays all the generated diagnostic messages. To view only the newly added warning message, navigate to the Comparator and select the previous simulation stage and the most recent simulation stage.

Save Diagnostic Messages from a Specific Operation as Reference

Save the diagnostic messages generated during an operation as a baseline and use this baseline for comparison with diagnostic messages in future operations.

The baseline diagnostic messages are saved as a JSON file. By using a baseline, you can compare diagnostic messages in the same MATLAB session, across different sessions, or even across different MATLAB versions. You can save multiple baselines for a model and import them to the Diagnostic Viewer for comparison.

Use these steps to save and compare diagnostic messages using the baseline:

1. To save the diagnostic messages as a baseline, navigate to the Viewer in the Diagnostic Viewer. Select any stage and click the export and save as baseline button. Save the baseline file using the default filename DiagnosticDemo_Simulation_Baseline.json or another filename. When saving a baseline, include a keyword in the filename, such as "simulation" or "build," to identify the contents of the file for future comparisons. By default, the baseline file is saved in the model path.

2. Open the DiagnosticDemo model in the current MATLAB session or start a new session.

3. Introduce an error in the model by setting the value of the Constant block as k.

4. Simulate the model.

5. To import the baseline, navigate to the Comparator in the Diagnostic Viewer. From the second drop-down menu for stage selection, select Import Baseline option. Open the saved baseline file.

6. To compare the diagnostic messages with the baseline, select the imported baseline and the most recent simulation stage from the drop-down menus.

See Also

Tools

Topics