Main Content

Scoped Simulink Function Blocks in Models

The scope of a Simulink® function is defined in the context of a model. If you place a Simulink Function block in a model at the root level, the function is scoped to the model by default. The Trigger block Function visibility parameter is set to scoped. Access the function with a function caller located:

  • Within the model hierarchy containing the function. Call the function without qualifying the function name.

  • Outside the model. Call the function by qualifying the function name with the Model block instance name, not the model file name.

Setting Function visibility for a Simulink Function block to global allows you to access the function from anywhere in the model or a parent model. As a result, models with a Simulink Function block set to global cannot be multi-instanced because function names must be unique.

Resolve to a Function Hierarchically

Placing a scoped Simulink Function block within a model at the root level limits access to the function and removes the function name from the global name space.

  • Function caller located in the current model.

  • You cannot place a function caller inside a Model block and the Simulink Function block in the parent model,

    If you place a function caller inside a Model block, Simulink displays the following error:

    Block is unable to call the function with scope name because the function definition is not found.

    This error occurs because the model containing the caller does not know the name of the function. Function calls cannot cross model reference boundaries.

Resolve to a Function by Qualification

When you place a Simulink Function block in a Model block, the function name is not accessible outside the model. However, you can call a function by qualifying the function name with the Model block name. When a function caller resolves to a qualified function hierarchically, it looks for the Model block containing the function using the following rules:

  • Resolution Rule 1: Is the Model block in the current model with the function caller?

  • Resolution Rule 2: If the Model block is not in the current model or subsystem, is the Model block in a parent or grandparent model one or more levels above the function caller?

If a function caller resolves to a Model block with a scoped function, you can call the function by qualifying the function name:

  • Function caller located outside of the Model block one hierarchic level above the function. In this case, the function caller finds the Model block with the scoped function in the current model (Resolution Rule 1).

  • Function caller in a subsystem at the same hierarchic level as the function. In this case, the function caller didn't find the Model block in the current subsystem, but it found the Model block in the parent model (Resolution Rule 2).

  • You cannot place a Simulink Function block in one Model block and the function caller in another Model block.

    If you place a Simulink Function block in a referenced model and a function caller in another referenced model, Simulink displays the following error:

    Block is unable to call the function with scope name because the function definition is not found.

    This error occurs because the qualified function name using the Model block name is not visible to the model containing the caller.

If you want to access the function using this modeling pattern, see the section Function Caller Block in Referenced Model and Function and the section Function Caller in Separate Models in the topic Simulink Function Blocks in Referenced Models.

Multi-Instance Modeling with Simulink Functions

Setting Function visibility for a Simulink Function block to scoped encapsulates the function within the model, allowing you to multi-instance the model. Adding the model instance name to the function name creates a qualified function name that is unique within the parent model.

  1. Create a model containing Simulink Function blocks.

    By default, the Function visibility parameter for the Trigger block within the Simulink Function block is set to scoped.

  2. Reference the model with Simulink functions from multiple Model blocks. Add a Function-Call Subsystem block to schedule calls to the functions.

  3. Add Function Caller blocks to the Function-Call Subsystem block. Access the function in separate model instances by qualifying the function name with the block name.

  4. On the Debug tab, select Information Overlays . From the drop-down box, select Function Connectors .

    Tracing lines are drawn to help you navigate from a function caller to the function.

For a model using Simulink Function blocks with multiple instances, see Model Reusable Components Using Multiply Instanced Simulink Functions.

See Also

| | | |

Related Topics