This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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. When a function caller resolves to a function hierarchically, it looks for the function using the following rules:

  • Resolution Rule 1: Is the scoped Simulink Function block in the current model with the function caller?

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

If a function caller resolves to a function hierarchically, you can call the function without qualifying the function name.

  • Function caller located at the same hierarchic level as the function. In this case, the function caller finds the scoped function in the current model (Resolution Rule 1).

  • Function caller located in a Subsystem block one or more subsystem levels below the hierarchic level of the function. The function caller hierarchy cannot include a Model block since the function caller cannot cross model reference boundaries. In this case, the function caller didn't find the scoped function in the current subsystem, but it found the function in the parent model (Resolution Rule 2).

  • 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 an error. 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 an error. 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. From the Display menu, 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 Modeling Reusable Components Using Multiply Instanced Simulink Functions.

See Also

Blocks

Related Topics