Report Diagnostic Messages Programmatically
Thesldiagviewer
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. Thesldiagviewer.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 thesldiagviewer
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
orMException
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 messagesmy_stage = sldiagviewer.createStage('Analysis','ModelName','vdp');% Catch the error introduced in vdp as an exception.trysim卡('vdp');catcherrorend% Report the caught exception as warningsldiagviewer.reportWarning(error);% Report a custom info message to Diagnostic Viewersldiagviewer.reportInfo('My Info message');
Log Diagnostic Messages
You can use thesldiagviewer.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 filediary.txt
in the current directory.sldiagviewer.diary(filename)
: Toggles the logging state of the text file specified byfilename
.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 thediary.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'
.
Example to Log Diagnostic Messages
% Log messages using sldiagviewer.diarysldiagviewer.diary open_system('vdp') rtwbuild('vdp')% Open diary.txt to view logs.### Startingbuildprocedureformodel:vdp### Buildprocedureformodel:'vdp'abortedduetoanerror....% Log messages using sldiagviewer.diary(filename)sldiagviewer.diary('C:\MyLogs\log1.txt')% Log messages using sldiagviewer.diary(toggle)sldiagviewer.diary('C:\MyLogs\log1.txt')% Switch on logging and specify a log file.open_system ('vdp') rtwbuild('vdp') sldiagviewer.diary('off')% Switch off logging.open_system ('sldemo_fuelsys') rtwbuild('sldemo_fuelsys') sldiagviewer.diary('on')% Resume logging in the previously specified log file.% Log messages using sldiagviewer.diary(filename,'UTF-8')sldiagviewer.diary('C:\MyLogs\log1.txt','UTF-8')