Simulink.ProtectedModel.Callback - Callback code that executes in response to protected model events - MATLAB (original) (raw)
Callback code that executes in response to protected model events
Description
For a specific protected model functionality, theSimulink.ProtectedModel.Callback
object specifies code to execute in response to an event. The callback code can be a character vector of MATLABĀ® commands or a MATLAB script.
When you create a protected model, to specify callbacks, call the Simulink.ModelReference.protect function with the'Callbacks'
option. The value of this option is a cell array of Simulink.ProtectedModel.Callback
objects.
Creation
Syntax
Description
Simulink.ProtectedModel.Callback(`Event`,`AppliesTo`,`CallbackText`)
creates a callback object for a specific protected model functionality and event. TheCallbackText
specifies MATLAB commands to execute for the callback.
Simulink.ProtectedModel.Callback(`Event`,`AppliesTo`,`callbackFile`)
creates a callback object for a specific protected model functionality and event. TheCallbackFileName
specifies a MATLAB script to execute for the callback. The script must be on the MATLAB path.
Properties
Protected model functionality that the event applies to, specified as one of these values:
'CODEGEN'
: Code generation.'SIM'
: Simulation.'VIEW'
: Read-only web view.'AUTO'
: If the event is'PreAccess'
, the callback executes for each functionality. If the event is'Build'
, the callback executes for only'CODEGEN'
functionality.
Example: 'SIM'
MATLAB script to execute in response to an event, specified as a character vector or string scalar. The script must be on the MATLAB path.
Example: 'pmCallback.m'
MATLAB commands to execute in response to an event, specified as a character vector or string scalar.
Example: 'A = [15 150];disp(A)'
Event that triggers the callback, specified as one of these options:
'PreAccess'
: Callback code is executed before simulation, build, or read-only viewing.'Build'
: Callback code is executed before build. Valid for only'CODEGEN'
functionality.
Example: 'PreAccess'
Option to override the protected model build process, specified as a Boolean value. The override option applies only to a callback object that you define for a 'Build'
event for the 'CODEGEN'
functionality. You set this option by using thesetOverrideBuild
method.
Object Functions
Examples
Create the callback object.
pmCallback = Simulink.ProtectedModel.Callback('PreAccess',...
'SIM','disp(''Hello world!'')')Protect the model
sldemo_mdlref_counter
and specify the callback object.
openExample('sldemo_mdlref_counter');
Simulink.ModelReference.protect('sldemo_mdlref_counter',...
'Callbacks',{pmCallback})Simulate the model
sldemo_mdlref_basic
, which references the protected model that you created.
openExample('sldemo_mdlref_basic');
sim('sldemo_mdlref_basic')
For each instance of the protected model reference in the top model, the output is displayed.
Hello world!
Hello world!
Hello world!On the MATLAB path, create a callback script
pm_callback.m
that contains this code:Create a callback object that uses the script for the callback code. Protect the model
sldemo_mdlref_counter
and specify the callback object.
pmCallback = Simulink.ProtectedModel.Callback('Build',...
'CODEGEN','pm_callback.m')
Simulink.ModelReference.protect('sldemo_mdlref_counter',...
'Mode', 'CodeGeneration','Callbacks',{pmCallback})
The callback script executes during the code generation phase of the model protection process.Generate code for the model
sldemo_mdlref_basic
, which references the protected model that you created.
slbuild('sldemo_mdlref_basic')
During the code generation phase for the referenced protected model, code inpm_callback.m
executes.
Version History
Introduced in R2016a