Main Content

Simulink.ProtectedModel.Callback

Callback code that executes in response to protected model events

Description

For a specific protected model functionality, the Simulink.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

Description

example

Simulink.ProtectedModel.Callback(Event,AppliesTo,CallbackText) creates a callback object for a specific protected model functionality and event. The CallbackText 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. The CallbackFileName specifies a MATLAB script to execute for the callback. The script must be on the MATLAB path.

Properties

expand all

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 the setOverrideBuild method.

Object Functions

setOverrideBuildOverride protected model build

Examples

collapse all

  1. Create the callback object.

    pmCallback = Simulink.ProtectedModel.Callback('PreAccess',...
    'SIM','disp(''Hello world!'')')
    
  2. Protect the model sldemo_mdlref_counter and specify the callback object.

    Simulink.ModelReference.protect('sldemo_mdlref_counter',...
    'Callbacks',{pmCallback})
    
  3. Simulate the model sldemo_mdlref_basic, which references the protected model that you created.

    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!
  1. On the MATLAB path, create a callback script pm_callback.m that contains this code:

    disp('Hello world!')
  2. 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.

  3. 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 in pm_callback.m executes.

Introduced in R2016a