Main Content

Report Diagnostic Messages Programmatically

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

You can use these functions to report the diagnostic messages programmatically:

  • Function to create a stage: sldiagviewer.createStage

  • Functions to report diagnostic messages:

    • sldiagviewer.reportError

    • sldiagviewer.reportWarning

    • sldiagviewer.reportInfo

  • Function to log the diagnostics: sldiagviewer.diary

Create Diagnostic Stages

In the Diagnostic Viewer, errors, warnings, and information messages are displayed in groups based on the operation, such as model load, simulation, and build. These groups are called stages. The sldiagviewer.createStage function enables you to create stages. You can also create child stages for a stage object. A parent stage object must be active to create a child stage. When you create a stage object, Simulink® initializes a stage. When you close the stage object, Simulink ends the stage. If you delete a parent stage object, the corresponding parent and its child stage close in the Diagnostic Viewer. The syntax for creating a stage is:

stageObject = sldiagviewer.createStage(StageName,'ModelName',ModelNameValue)

In this syntax,

  • StageName specifies the name of a stage and is a required argument, for example, 'Analysis'.

  • Use the 'ModelName', ModelNameValue pair to specify the model name of a stage, for example 'ModelName', 'vdp'. All the child stages inherit the model name from their parent.

Example to Create Stage

my_stage = sldiagviewer.createStage('Analysis','ModelName','vdp');

Report Diagnostic Messages

You can use the sldiagviewer functions to report error, warning, or information messages in the Diagnostic Viewer. The syntaxes for reporting diagnostic messages are:

  • sldiagviewer.reportError(Message): Reports the error messages.

  • sldiagviewer.reportWarning(Message): Reports the warnings.

  • sldiagviewer.reportInfo(Message): Reports the information messages.

Message describes the error, warning, or build information and is a required argument. Message can have values in these formats:

  • String

  • MSLException or MException object

Optionally, you can use the 'Component' argument and its corresponding value in the syntax to specify the component or product that generates the message, for example, 'Simulink' and 'Stateflow'.

Example to Report Diagnostics

% Create a Stage to display all the messages

my_stage = sldiagviewer.createStage('Analysis', 'ModelName', 'vdp');

% Catch the error introduced in vdp as an exception.

catch error
% Report the caught exception as warning


% Report a custom info message to Diagnostic Viewer

sldiagviewer.reportInfo('My Info message');

Log Diagnostic Messages

You can use the sldiagviewer.diary function to log the simulation warning, error, and build information to a file. The syntaxes for generating log files are:

  • sldiagviewer.diary: Intercepts the build information, warnings, and errors transmitted to the Diagnostic Viewer and logs them to a text file diary.txt in the current directory.

  • sldiagviewer.diary(filename): Toggles the logging state of the text file specified by filename.

  • sldiagviewer.diary(toggle): Toggles the logging ability. Valid values are 'on' and 'off'. If you have not specified a log file name, the toggle setting applies to the last file name that you specified for logging or to the diary.txt file.

  • sldiagviewer.diary(filename,'UTF-8'): Specifies the character encoding for the log file.

In this syntax,

  • filename specifies the file to log the data to.

  • toggle specifies the logging state 'on' or 'off'.

Log Diagnostic Messages

% Start logging build information and simulation warnings and errors to diary.txt

set_param('vdp/Mu','Gain', 'xyz') 
set_param('vdp', 'SimulationCommand', 'Start') 
% This introduces an error and do UI simulation which you can see in the diary log

% Open diary.txt to view logs.

%### Starting build procedure for model: vdp
%### Build procedure for model: 'vdp' aborted due to an error.

% Set up logging to a specific file
% Make sure you have write permission for this location


% Switch the logging state of a file

sldiagviewer.diary('C:\MyLogs\log2.txt') % Switch on logging and specify a log file.
set_param('vdp/Mu', 'Gain', 'xyz') 
set_param('vdp', 'SimulationCommand', 'Start')

sldiagviewer.diary('off') % Switch off logging.
% Any operation you do after the previous command will not be logged

sldiagviewer.diary('on') % Resume logging in the previously specified log file.

% Specify the filename to log to and character encoding to be used

Related Topics