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.

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.

InitFcn in Model Properties dialog box

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, the InitFcn model callback status can 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 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 or delete_line in the InitFcn 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.

Model with Variant Subsystem and Variant Source blocks

In this model, the InitFcn model 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 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.

InitFcn in Block Properties dialog box

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

Related Topics

Go to top of page