Initialization Function
An initialization function (InitFcn
) is a type of callback that is
executed or evaluated at the beginning of model compilation. You can specify the
InitFcn
callback as a model or block callback.
Note
Variant controls can be defined only in a model InitFcn
callback.
Model InitFcn
Callback
Use the model InitFcn
callback to initialize parameters and
environment settings that are specific to the model, for example:
Variables used in model parameters
License checks for the required software
In the Simulink® Toolstrip, on the Modeling tab, click the Model
Settings arrow, then click Model Properties. In the Model
Properties dialog box, the model InitFcn
callback is on the
Callbacks tab.
Do not use the model InitFcn
callback to:
Get the simulation status. If you simulate a model with the
slbuild
function or use software-in-the-loop (SIL) or rapid accelerator mode, the modelInitFcn
callback status may show as'stopped'
.Run simulation commands. For example, do not use commands such as
set_param(ModelName,'SimulationCommand','start')
orset_param(ModelName,'SimulationCommand','update')
in the modelInitFcn
callback.Modify other models. Similarly, the block
InitFcn
callback of a Model block must not modify the parameters or structure of the referenced model.Overwrite any variables used in a referenced model. For example, if the top model and a referenced model use the variable k, the model
InitFcn
callback of the top model must not modify k for the referenced model. In such modeling patterns, use different variable names or a data dictionary.Edit the structure of the model, except to define variant controls. For example, do not use functions such as
add_line
ordelete_line
in the modelInitFcn
callback.
If you use the InitFcn
callback for a model, edit-time checking for
missing variables in block parameters is disabled for the entire model.
For examples of the model InitFcn
callback with variants, open the
sldemo_variant_initFcn
model. Then, double-click the green block to
open a model that uses the recommended workflow. The model that opens contains
Variant Subsystem and Variant Source blocks.
In this model, the model InitFcn
callback is used to initialize
parameters for the model.
While you can define these parameters in the model PreLoadFcn
or
PostLoadFcn
callbacks, these callbacks execute before or after the
model loads. Suppose you clear the parameters from the MATLAB® workspace and simulate the model. The model PreLoadFcn
and
PostLoadFcn
callbacks do not re-create the parameters. When you define
the parameters in the model InitFcn
callback, model update and simulation
re-create the parameters.
Block InitFcn
Callback
Use the block InitFcn
callback to initialize parameters and settings
of the block.
Right-click the block. Then, click Properties. In the Block
Properties dialog box, the block InitFcn
callback is on the
Callbacks tab.
Do not use the block InitFcn
callback to:
Modify the parameters (or variables) of other blocks, except in the case where you use the
InitFcn
callback of a Subsystem block to modify the parameters of its direct child blocks.Make structural changes. For example, do not use functions such as
add_block
ordelete_block
in the blockInitFcn
callback.Modify any global variables or variables in the model workspace that another block uses.
Modify the parameters, structure, or settings of a model referenced by a Model block.
If you use an InitFcn
callback for a block, edit-time checking for
missing variables in block parameters is disabled for that block.