Main Content

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.

InitFcn in Model Properties dialog box

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 model InitFcn callback status may show as 'stopped'.

  • Run simulation commands. For example, do not use commands such as set_param(ModelName,'SimulationCommand','start') or set_param(ModelName,'SimulationCommand','update') in the model InitFcn 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 or delete_line in the model InitFcn 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.

Model with Variant Subsystem and Variant Source blocks

In this model, the model InitFcn callback is used to initialize parameters for the model.

InitFcn that defines V, W, W.Value, and W.DataType

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.

InitFcn in Block Properties dialog box

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 or delete_block in the block InitFcn 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.

Related Topics