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.

example

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.

example

Properties

expand all

Protected model functionality that the event applies to, specified as one of these values:

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:

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

collapse all

  1. Create the callback object.
    pmCallback = Simulink.ProtectedModel.Callback('PreAccess',...
    'SIM','disp(''Hello world!'')')

  2. Protect the modelsldemo_mdlref_counter and specify the callback object.
    openExample('sldemo_mdlref_counter');
    Simulink.ModelReference.protect('sldemo_mdlref_counter',...
    'Callbacks',{pmCallback})

  3. Simulate the modelsldemo_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!

  4. On the MATLAB path, create a callback scriptpm_callback.m that contains this code:

  5. Create a callback object that uses the script for the callback code. Protect the modelsldemo_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.

  6. Generate code for the modelsldemo_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