Main Content

sldiagviewer.Comparator.compareWithBaseline

Compare diagnostic details with saved baseline

Since R2025a

    Description

    compBaseline = sldiagviewer.Comparator.compareWithBaseline(baselinePath,diagMsgStage) returns a structure compBaseline that contains the details of the diagnostics that are added or removed in diagMsgStage as compared to baselinePath, where diagMsgStage contains the diagnostic details generated during a model operation and baselinePath is the saved baseline file. For information on saving baselines, see sldiagviewer.DiagnosticReceiver.saveBaseline.

    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;

    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.

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

    stage1 = getDiagnostics(rx1);

    Save the diagnostic details from the stage1 simulation as a baseline in a JSON file.

    baseline_path = "D:\Projects\Simulink\Systems\baseline_simulation.json";
    sldiagviewer.DiagnosticReceiver.saveBaseline(baseline_path,stage1)
    

    Baseline saved successfully.

    Delete the DiagnosticReceiver object rx1.

    delete(rx1);

    Start a new MATLAB session or open the model again.

    close_system(model,0);
    open_system(model);

    Create a new DiagnosticReceiver object rx2.

    rx2 = sldiagviewer.DiagnosticReceiver;

    Simulate the model.

    sim(model);

    Get the diagnostic details generated during model simulation by using the DiagnosticReceiver object rx2.

    stage2 = getDiagnostics(rx2);

    Delete the DiagnosticReceiver object rx2 to close the current diagnostic stage.

    delete(rx2);

    Compare the stage2 diagnostic details with the saved baseline and display the comparison results.

    comp_baseline = sldiagviewer.Comparator.compareWithBaseline(baseline_path,stage2)
    sldiagviewer.Comparator.displayResult(comp_baseline)
    

    Diagnostics Added

    Messages:

    No new diagnostics

    Errors: 0

    Warnings: 0

    Diagnostics Removed

    Errors: 0

    Warnings: 5

    The results show that no new diagnostics are added and five warnings are removed.

    Input Arguments

    collapse all

    Absolute path to the baseline file, specified as a string scalar or character vector.

    Example: "D:\Projects\Simulink\Systems\baseline_simulation.json"

    Data Types: string | char

    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 messages between two model operations, returned as a structure.

    Alternatives

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

    Version History

    Introduced in R2025a