Main Content

Report Diagnostic Messages Programmatically

The sldiagviewer functions enable you to generate, display, and log diagnostic messages in the Diagnostic Viewer.

By using these functions, you can programmatically:

  • Create new diagnostic stages.

  • Report diagnostic messages in the Diagnostic Viewer.

  • Log diagnostic messages in a text file.

Create Diagnostic Stages

The Diagnostic Viewer organizes and displays the error, warning, and information messages in the Diagnostic Message Pane in different stages. Each stage represents a runtime operation, such as model load, simulation, build, or diagram update. As more operations occur, new stages are created. For cases involving multiple operations, child stages are created to form a hierarchical structure.

To initialize a stage, to log the diagnostic messages for the upcoming runtime operation, create a stage object by using the sldiagviewer.createStage function. If you create a new stage object without ending the current stage, the new stage is a child stage of the current stage.

To end a stage, close the stage object. If you delete a parent stage object, the parent and its child stages close in the Diagnostic Viewer.

Create Stage to Display Diagnostic Messages

Create a stage to display diagnostic messages in the Diagnostic Viewer using sldiagviewer.createStage.

Load the model DiagnosticDemo.

model = "DiagnosticDemo";
load_system(model);

Create a stage to display the diagnostic messages.

myStage = sldiagviewer.createStage("Analysis","ModelName",model);

After you create the stage, you can report the diagnostics of your next Simulink® operation in the Diagnostic Viewer in the Analysis stage. For more information, see Diagnostic Viewer.

Report Diagnostic Messages

Use the sldiagviewer.reportError, sldiagviewer.reportWarning, and sldiagviewer.reportInfo functions to report error, warning, and information messages, respectively, in the Diagnostic Viewer.

Optionally, you can specify the name of the component or the product that generates the diagnostic message, such as Simulink, Model Advisor, or Stateflow.

Report Error Message

Display error messages in the Diagnostic Viewer using sldiagviewer.reportError.

Load the model DiagnosticDemo.

model = "DiagnosticDemo";
load_system(model);

Create a stage to display the diagnostic messages.

myStage = sldiagviewer.createStage("Analysis","ModelName",model);

Introduce errors inside the try block and catch the resulting errors in the catch block.

try 
 i = p; 
catch error 
end 

Report the error message in the Diagnostic Viewer.

output = sldiagviewer.reportError(error)

The diagnostic window opens for the model and displays the error message in red in the Analysis stage.

Log Diagnostic Messages in a File

Use the sldiagviewer.diary function to log diagnostic messages and build information in a text file.

By default, sldiagviewer.diary logs the diagnostic messages in a text file diary.txt located in the current folder using the default character encoding of your system. You can also specify a filename to log messages and set character encoding to UTF-8. For more information, see Log Diagnostic Messages in Specific File and Specify Character Encoding of Log File. You can also switch the logging state to on or off in the current log file.

Toggle File Logging State

Create a log by file using sldiagviewer.diary and toggle the logging state.

Create the log file diary.txt and turn on logging.

sldiagviewer.diary

Load the model DiagnosticDemo.

model = "DiagnosticDemo";
open_system(model);

Introduce errors into the model.

set_param("DiagnosticDemo/Gain","Gain","xyz");

Simulate the model.

set_param(model,"SimulationCommand","Start") 

The errors are logged in diary.txt.

Again, introduce errors into the model.

set_param("DiagnosticDemo/Gain","Gain","abc");

Turn off logging.

sldiagviewer.diary("off")

Simulate the model.

set_param(model,"SimulationCommand","Start")

The errors are not logged in diary.txt.

See Also

Tools

Related Topics