Initialization Function

Initialization function (InitFcn) is a type of callback that is executed or evaluated at the beginning of model compilation. You can use InitFcn in a model (model InitFcn) or a block (block InitFcn).

Note

Variant controls can be defined only in model InitFcn callback.

Model InitFcn

The model InitFcn callback is used to initialize parameters and environment settings that are specific to the model used.

Note

It is not recommended to use the model InitFcn callback to get the simulation status. If you simulate a model in rtwbuild, or SIL(software-in-the-loop), or Rapid Accelerator mode, the model InitFcn callback status may show as 'stopped'. The model InitFcn callback must be used only to initialize parameters and environment settings that are specific to the model used.

Best practices for using Model InitFcn

  • Do use model InitFcn to initialize data required for the model. For example, to initialize:

    • Variables used in model parameters

    • License checks for the required software

  • Do not use model InitFcn to modify models other than self. This also means that the block InitFcn of Model block must not modify the parameters (and structure) of the referenced model.

  • Do not use model InitFcn in the top model to overwrite any variable used in the referenced model. For example, if top and the referenced models use the variable ‘k’, the model InitFcn of the top model must not modify ‘k’ of the referenced model. In such modeling patterns, it is recommended that you use different variable names. Alternatively, you can use data dictionary.

  • Do not use simulation commands in model InitFcn. For example, using commands like, set_param(ModelName, 'SimulationCommand', 'start') or set_param(ModelName, 'SimulationCommand', 'update') in the model InitFcn are not recommended.

If you use the InitFcn callback for the model, edit-time checking for missing variables in block parameters is disabled for the entire model.

Variants with Model InitFcn

You can use model properties to define the callbacks for a model. For example, PreLoadFcn, PostLoadFcn, InitFcn callbacks. Model properties are used to view the model information, description, history, and callback functions. You can use the Property Inspector to view and edit model version properties, description history, and callback functions. For more information on Model properties, see Manage Model Properties.

Consider a model with Variant Subsystem and Variant Source blocks. For example, Variant - InitFcn.

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

Similarly, these parameters can also be defined in PreLoadFcn or PostLoadFcn.

However, when the parameters are defined in InitFcn callback and if MATLAB® workspace is cleared using the Clear command, the items in the workspace are cleared and they are re-created when you simulate the model. The items cleared will not be recreated when parameters are defined in PreLoadFcn or PostLoadFcn.

Block InitFcn

The block InitFcn callback is used to initialize block specific parameters and settings.

Best practices for using Block InitFcn

  • Do not use block InitFcn to modify the parameters (or variables) of blocks other than self.

  • Do not use block InitFcn on a child block to modify the parameters of the parent subsystem block or other child blocks. However, you can use block InitFcn on a parent subsystem block to modify the parameters of the direct child blocks.

  • Do not use block InitFcn to make structural changes like adding or deleting block (add_block or delete_block).

  • Do not use block InitFcn in the Model block to modify the parameters (and structure) of the referenced model.

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