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
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.
my_stage = sldiagviewer.createStage('Analysis','ModelName','vdp');
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'
.
% 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');
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'
.
% Start logging build information and simulation warnings and errors to diary.txt sldiagviewer.diary open_system('vdp') 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 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', 'Gain', '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')