This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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.

try
sim('vdp');
catch error
% Report the caught exception as warning

sldiagviewer.reportWarning(error);
end

% 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

sldiagviewer.diary
open_system('vdp')
set_param('vdp/Mu', 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

sldiagviewer.diary('C:\MyLogs\log1.txt') % 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.
open_system('vdp')
set_param('vdp/Mu', xyz) 
set_param('vdp', 'SimulationCommand', 'Start')

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

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

% Specify the filename to log to and character encoding to be used
sldiagviewer.diary('C:\MyLogs\log3.txt','UTF-8')

Related Topics