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.
InitFcn
Model Callback
Use the InitFcn
model 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 InitFcn
model callback is on the
Callbacks tab.
Do not use the InitFcn
model 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, theInitFcn
model callback status can 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
InitFcn
block 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
InitFcn
model 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 theInitFcn
model 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 InitFcn
model 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 InitFcn
model callback is used to initialize
parameters for the model.
While you can define these parameters in the PreLoadFcn
or
PostLoadFcn
model callbacks, these callbacks execute before or after
the model loads. Suppose you clear the parameters from the MATLAB® workspace and simulate the model. The PreLoadFcn
and
PostLoadFcn
model callbacks do not re-create the parameters. When you
define the parameters in the InitFcn
model callback, model update and
simulation re-create the parameters.
InitFcn
Block Callback
Use the InitFcn
block callback to initialize parameters and settings
of the block.
Right-click the block. Then, click Properties. In the Block
Properties dialog box, the InitFcn
block callback is on the
Callbacks tab.
Do not use the InitFcn
block 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 theInitFcn
block 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.