Reuse Multiple Instances of a Simulink Function - MATLAB & Simulink (original) (raw)

Main Content

This example shows how to reuse instances of the same Simulink® function. The model slexCounterFunctionMdlRef contains the update function, which implements a counter. Subsystems CounterA and CounterB each contain an instance of the counter, allowing the model to reuse the function logic.

Open the model.

open_system('slexMultiInstanceFunctionsExample');

Simulink model with an UpdateCounterA subsystem, UpdateCounterB subsystem, a Model block referencing CounterA, a Model block referencing CounterB, a Constant block with value 0, a Constant block with value 1000, and multiple Pulse Generater blocks.

The function in the slexMultiInstanceFunctionsExample model is the update function which is defined in the slexCounterFunctionMdlRef subsystem.

To view the Simulink Function block, open the CounterA subsystem.

open_system('slexMultiInstanceFunctionsExample/CounterA');

A Simulink Function block with function prototype, output equals update of upper, input, and lower.

Subsystems CounterA and CounterB each contain an instance of the counter implemented in the slexCounterFunctionMdlRef model. Since there are multiple instances of the counter, the model can reuse the function logic and can call each instance from multiple blocks.

The function callers in the slexMultiInstanceFunctionsExample model are in the UpdateCounterA and UpdateCounterB subsystems. The calling notation CounterA.update and CounterB.update distinguishes the two different instances of the counter logic.

To view the callers for CounterA, double-click the UpdateCounterA subsystem. The Function Caller blocks in the subsystem qualify the function name with the Model block name. In this case, CounterA is the Model block name and update is the function name.

sim('slexMultiInstanceFunctionsExample');

See Also

Argument Inport | Argument Outport | Function Caller | Model | Simulink Function

Topics