Main Content

Conditionally Execute Simulink Functions

With variant blocks, you can conditionalize functions defined using Simulink Function blocks. A variant condition from a variant block does not propagate inside a Simulink Function block. Instead, it stays on the boundary and controls the entire Simulink Function block. For more information, see Simulink Function.

Explore the Model

Consider the model slexVariantSimulinkFunctionInherit.

open_system("slexVariantSimulinkFunctionInherit")

In this example, the function-call port block within the Simulink Function block has the Enable variant condition option selected and the Variant control parameter is set to (inherit). During simulation, the Simulink Function block inherits the logical OR of the variant conditions, A == 1 and B == 1, from the corresponding Function Caller blocks Function Caller1 and Function Caller2. The Generate preprocessor conditionals parameter value that determines whether to include only the active choice, or both active and inactive choice in the generated code is also inherited from the Function Caller blocks.

sim("slexVariantSimulinkFunctionInherit")

Consider the model slexVariantSimulinkFunctionSpecify.

open_system("slexVariantSimulinkFunctionSpecify")

In this example, the function-call port block within the Simulink Function block has the Enable variant condition option selected and the Variant control parameter is set to V == 1. During simulation, the variant condition V == 1 propagates from the Variant Source block to the Function Caller block. To ensure the agreement between the function caller and its callee, Simulink® checks if the propagated variant condition on the Function Caller block is same as the variant condition specified in the Simulink Function block during simulation. The simulation fails if the variant condition that propagates to the Function Caller block is not same as the variant condition propagated to the Simulink Function block.

Note

The Configure C Step Function Interface dialog box allows you to configure the input and output data types, the function name, and other parameters for the generated code. However, if the input and output ports of the subsystem share an argument name and have propagated variant conditions, you cannot customize the generated C code for those ports and you must use the default settings.

Limitations

  • Variant condition propagation from a Simulink Function block inside Stateflow block is not supported.

  • Argument Inport and Argument Outport blocks interfacing with Simulink Function blocks cannot be connected to Variant Source or Variant Sink variant blocks.

See Also

Propagate Variant Conditions to Define Variant Regions with Variant Blocks