Main Content

sldiagviewer.Comparator.compare

Compare diagnostic details between model operations

Since R2025a

    Description

    comparison = sldiagviewer.Comparator.compare(diagMsgStage1,diagMsgStage2) returns a structure comparison that contains the details of diagnostics that are added or removed in diagMsgStage2 as compared to diagMsgStage1. Both diagMsgStage1 and diagMsgStage2 must contain diagnostic details generated from two model operations of the same type. For example, diagnostics from a simulation operation must be compared with diagnostics from another simulation operation.

    example

    Examples

    collapse all

    Open the model DiagnosticDemo.

    model = "DiagnosticDemo";
    open_system(model);

    Create a sldiagviewer.DiagnosticReceiver object. 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 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 diagnostics 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}}'
    

    Input Arguments

    collapse all

    Diagnostic details of a model run-time operation, specified as a cell array of MSLDiagnostic objects. To get the diagnostic details of a model operation use the getDiagnostics function.

    Data Types: cell

    Diagnostic details of a model run-time operation, specified as a cell array of MSLDiagnostic objects. To get the diagnostic details of a model operation use the getDiagnostics function.

    Data Types: cell

    Output Arguments

    collapse all

    Diagnostics that are added or removed when comparing diagnostic details between two model operations, returned as a structure.

    Alternatives

    You can compare the diagnostics using the Comparator available in the Diagnostic Viewer. For more information, see Compare Diagnostic Messages Between Model Simulations.

    Version History

    Introduced in R2025a