When you create a protected model, you can customize its behavior by defining callbacks. Callbacks specify code that executes when you view, simulate, or generate code for the protected model. You cannot have protected model callbacks with HDL code generation support enabled for a protected model. To learn more about HDL code generation limitations, see Protected Model Restrictions for HDL Code Generation (HDL Coder).
A protected model user cannot view or modify a callback. If a model references a protected model with callbacks, you cannot protect the model.
To create a protected model with callbacks:
Define Simulink.ProtectedModel.Callback
objects for each callback.
To create your protected model, call the Simulink.ModelReference.protect
function. Use the 'Callbacks'
option to specify a cell array
of callbacks to include in the protected model.
To create and define a protected model callback, create a Simulink.ProtectedModel.Callback
object. Callback objects specify:
The code to execute for the callback. The code can be a character vector of MATLAB® commands or a script on the MATLAB path.
The event that triggers the callback. The event can be
'PreAccess'
or 'Build'
.
The protected model functionality that the event applies to. The
functionality can be 'CODEGEN'
,
'SIM'
, 'VIEW'
, or
'AUTO'
. If you select 'AUTO'
,
and the event is 'PreAccess'
, the callback applies to
each functionality. If you select 'AUTO'
, and the
event is 'Build'
, the callback applies only to
'CODEGEN'
functionality. If you do not select a
functionality, the default behavior is 'AUTO'
.
The option to override the protected model build process. This option
applies only to 'CODEGEN'
functionality.
You can create only one callback per event and per functionality.
You can define the code for a callback by using either a character vector of MATLAB commands or a script on the MATLAB path. When you write callback code, follow these guidelines:
Callbacks must use MATLAB code (.m
or .p
).
The code can include protected model functions or a MATLAB command that does not require loading the model.
Callback code must not call out to external utilities unless those utilities are available in the environment where the protected model is used.
Callback code cannot reference the source protected model unless you are using protected model functions.
You can use the Simulink.ProtectedModel.getCallbackInfo
function in callback code to get information on the protected model. The function
returns a Simulink.ProtectedModel.CallbackInfo
object
that provides the protected model name and the names of submodels. If the callback
is specified for 'CODEGEN'
functionality
and 'Build'
event, the object provides the target
identifier and model code interface type ('Top model'
or
'Model reference'
).
This example creates a protected model with a callback for code generation.
On the MATLAB path, create a callback script,
pm_callback.m
, containing:
s1 = 'Code interface is: '; cbinfobj = Simulink.ProtectedModel.getCallbackInfo(... 'sldemo_mdlref_counter','Build','CODEGEN'); disp([s1 cbinfobj.CodeInterface]);
Create a callback that uses the script.
pmCallback = Simulink.ProtectedModel.Callback('Build',... 'CODEGEN', 'pm_callback.m');
Create the protected model and specify the code generation callback.
Simulink.ModelReference.protect('sldemo_mdlref_counter',... 'Mode', 'CodeGeneration','Callbacks',{pmCallback})
Build the protected model. Before the build, the callback displays the code interface.
rtwbuild('sldemo_mdlref_basic')
Simulink.ModelReference.protect
| Simulink.ProtectedModel.Callback
| Simulink.ProtectedModel.getCallbackInfo