Main Content

Call Simulink Functions in Other Models Using Function Ports

Function ports allow a referenced model to call a Simulink® function defined in a second referenced model. A model can issue a function call through an invoking function port to invoke a function defined and exported in another model with an exporting function port block. You can use function ports to model client and server components in a distributed service architecture as well as to facilitate data sharing using a functional interface between component models.

A model that uses function ports must be an export-function model. See Export-Function Models Overview.

Simulink canvas with 2 model blocks. One model block is named "mClient1" and displays a port labeled "client". The other model block is named "mServer1" and displays a port labeled "server". The 2 ports are connected by a signal line, with a solid black circle at the end of the line next to the port labeled "server".

To create an exporting function port, add a Function Element block to your model, specify a name for the port, and specify one or more function elements.

Block dialog for Function Element block

For each function element, add a Simulink Function block to the model and implement the desired algorithm inside the Simulink Function block. To connect the Simulink Function block to the function port, set the Function visibility parameter of the Trigger block inside the Simulink Function block to port, and enter the port name in the Scope to port field.

Block dialog for Trigger block, with 'Treat as Simulink Function' check box selected. The parameter 'Function visibility' is set to 'port' and the parameter 'Scope to port' is set to 'serverPort'. Both parameters are highlighted.

Because the block is scoped to the port, it is not visible elsewhere within the model hierarchy and can be called only through the port. The block icon indicates the port scope.

Simulink canvas with a Simulink Function block with "scoped: serverPort" superimposed on the block icon, and a Function Element block with 3 function elements labeled "serverPort". A function connector line connects the Simulink Function block to one of the function elements.

To create an invoking function port, add a Function Element Call block to your model, specify a name for the port, and specify one or more function elements.

Block dialog of Function Element Call block

For each function element, add a Function-Call Subsystem block, and place a Function Caller block inside the subsystem. To associate the Function Caller block with the port, use dot notation: PortName.FunctionElementName. The block icon indicates the association with the port.

Simulink canvas with 2 Function Element Call blocks labeled "clientPort.f0" and "clientPort.f1" and a Function-Call Subsystem. Inside the subsystem there is a Function Caller block with "clientPort.f0()" superimposed on the block icon. The Function Caller block is connected to the first Function Element Call block via a function connector line.

You can associate function ports with service interfaces authored in System Composer™ using the Function Element and Function Element Call blocks corresponding to the port.

When you reference a model with a function port, the port displays on Model block. To allow functions to be called through the ports, add a line between them. A ball icon appears at the end of the line next to the exporting function port. The attributes of the ports must match in number, names, and prototypes of function elements. Function ports can connect only to function ports and not to any other signal.

Simulink canvas with a Model block referencing model "serverModel" displaying a port labeled "serverPort" and a Model block referening model "clientModel" displaying a port labeled "clientPort". The ports are connected by a line, with a filled-in black circle at the end of the port labeled "serverPort".

See Also

| | | |

Related Topics