Compare Diagnostic Messages Between Model Simulations Programmatically
The functions of sldiagviewer.DiagnosticReceiver
object and sldiagviewer.Comparator
namespace enable you to programmatically compare diagnostic messages from various run-time operations of a Simulink® model.
By using these functions, you can:
Get diagnostics generated by a model from various run-time operations.
Compare diagnostics between any two run-time operations.
View newly added diagnostic messages and the count of the diagnostic messages that have been added and removed between model operations.
Save diagnostic details from an operation to use as a basis of comparison with diagnostic details from a subsequent operation in another MATLAB® session.
Convert the diagnostics comparison to a JSON format.
Compare Diagnostic Details from Two Simulations
Open the model DiagnosticDemo
.
model = "DiagnosticDemo";
open_system(model);
Create a sldiagviewer.DiagnosticReceiver
object to initiate a diagnostic stage. This object receives the diagnostic details from all model operations until the object is deleted.
rx1 = sldiagviewer.DiagnosticReceiver;
Simulate the model.
sim(model);
Get the diagnostic details generated during model simulation by using the DiagnosticReceiver
object rx1
.
stage1 = getDiagnostics(rx1);
Delete the DiagnosticReceiver
object rx1
and then create a new DiagnosticReceiver
object rx2
. This prevents diagnostic details from previous simulations from being carried over to rx2
. Otherwise, rx2
also includes diagnostic details received in rx1
.
delete(rx1); rx2 = sldiagviewer.DiagnosticReceiver;
Add a few sample warnings to the model by setting the StartFcn
model callback.
setWarningCmd = sprintf([ 'for i = 1:5\n', ... 'sldiagviewer.reportWarning("Sample warning.");\n', ... 'end']); set_param(model,'StartFcn',setWarningCmd);
Simulate the model again.
sim(model);
Warning: Sample warning.
Warning: Sample warning.
Warning: Sample warning.
Warning: Sample warning.
Warning: Sample warning.
Get the diagnostic details generated during model simulation by using the DiagnosticReceiver
object rx2
.
stage2 = getDiagnostics(rx2);
Delete the DiagnosticReceiver
object rx2
.
delete(rx2);
Compare the diagnostics from the two simulation stages. When you compare the diagnostics, a structure stores the details of the diagnostics that are added or removed in stage2
as compared to stage1
.
comparison = sldiagviewer.Comparator.compare(stage1,stage2)
comparison = struct with fields:
DiagnosticsAdded: [1×1 struct]
DiagnosticsRemoved: [1×1 struct]
Display the comparison results.
sldiagviewer.Comparator.displayResult(comparison)
Diagnostics Added Messages: Sample warning. Sample warning. Sample warning. Sample warning. Sample warning. Errors: 0 Warnings: 5 Diagnostics Removed Errors: 0 Warnings: 0
The results show that five new warnings are added and no diagnostic messages are removed. Displaying the results also shows the newly added diagnostic messages.
Convert the comparison structure to a JSON format.
comp_json = sldiagviewer.Comparator.convertToJson(comparison)
comp_json = '{"DiagnosticsAdded":{"Message":["Sample warning.","Sample warning.","Sample warning.","Sample warning.","Sample warning."],"Errors":0,"Warnings":5},"DiagnosticsRemoved":{"Errors":0,"Warnings":0}}'
Save and Compare Diagnostic Details from a Specific Operation
Save the diagnostic details from various run-time operations as baselines and use these baselines for comparison with diagnostic details in subsequent operations. You can use baselines to compare diagnostic details in the same MATLAB session, across different sessions, or even across different MATLAB versions.
Save the diagnostic details from the stage2
simulation as a baseline in a JSON file. When saving a baseline, include a word in the filename such as "simulation" or "build" to identify the contents of the file for future comparisons.
baseline_path = "D:\Projects\Simulink\Systems\baseline_simulation.json";
sldiagviewer.DiagnosticReceiver.saveBaseline(stage2,baseline_path)
Baseline saved successfully.
Start a new MATLAB session or open the model again.
close_system(model,0); open_system(model);
Create a new DiagnosticReceiver
object rx3
.
rx3 = sldiagviewer.DiagnosticReceiver;
Introduce a warning in the model by deleting the Scope block, and then simulate the model.
delete_block("DiagnosticDemo/Scope")
sim(model);
Warning: All blocks in block diagram '<a href="matlab:open_system ('DiagnosticDemo')">DiagnosticDemo</a>' are either virtual or have been removed by block reduction optimization or they are inactive variants, therefore there is nothing to simulate. Note, for code generation, block reduction optimization removes all diagram branches terminating in sink blocks that do not participate in code generation. For example, To Workspace blocks and potentially their sources are removed when the <a href="matlab:configset.internal.open('DiagnosticDemo','MatFileLogging');">MAT-file logging</a> is off.
Get the diagnostics from the simulation.
stage3 = getDiagnostics(rx3);
Delete the DiagnosticReceiver
object rx3
.
delete(rx3);
Compare the stage3
diagnostic details with the saved baseline and display the comparison results.
comp_baseline = sldiagviewer.Comparator.compareWithBaseline(baseline_path,stage3) sldiagviewer.Comparator.displayResult(comp_baseline)
Diagnostics Added
Messages:
All blocks in block diagram '
DiagnosticDemo
' are either virtual or have been removed by block reduction optimization or they are inactive variants, therefore there is nothing to simulate. Note, for code generation, block reduction optimization removes all diagram branches terminating in sink blocks that do not participate in code generation. For example, To Workspace blocks and potentially their sources are removed when the
MAT-file logging
is off.
Errors: 0
Warnings: 1
Diagnostics Removed
Errors: 0
Warnings: 5
close_system(model,0);
See Also
Tools
Functions
sldiagviewer.Comparator.compare
|sldiagviewer.Comparator.displayResult
|sldiagviewer.Comparator.compareWithBaseline
|sldiagviewer.Comparator.convertToJson