Main Content

Simulink Function

Graphically define a function with Simulink blocks

  • Simulink Function block

Libraries:
Simulink / User-Defined Functions
SimEvents

Description

The Simulink Function block is a subsystem block preconfigured as a starting point for graphically defining a function with Simulink® blocks. The block provides a text interface to function callers. You can call a Simulink Function block from a Function Caller block, a MATLAB Function block, or a Stateflow® chart. See Call a Simulink Function from a Model.

For a description of the block parameters, see Subsystem.

You can visualize Simulink Function calls in the Sequence Viewer. The viewer shows when calls were made with the argument and the return values.

You can select a Simulink Function or Function Caller block to highlight related blocks. If one or more of the related blocks are in a subsystem or referenced model, the related blocks and the Subsystem block or Model block that contains the related blocks are also highlighted.

To show a related block in an open diagram or new tab, pause on the ellipsis that appears after you select a Simulink Function or Function Caller block. Then, select Related Blocks Related Blocks button from the action bar. When multiple blocks correspond to the selected block, a list of related blocks opens. You can filter the list by entering a search term in the text box. After you select a related block from the list, window focus goes to the open diagram or new tab that shows the related block.

Function Interface

The function interface appears on the face of a Simulink Function block. Editing the block text adds and deletes Argument Inport blocks and Argument Outport blocks from the function definition. Editing also sets the Function name parameter in the Trigger block within the Simulink Function block.

For example, entering y = myfunction(u) on the face of a Simulink Function block adds one Argument Inport block (u) and one Argument Outport block (y) within the subsystem.

When calling a function using a Function Caller block, the parameter Function prototype in the Function Caller block must match exactly the function interface you specify on the Simulink Function block. This match includes the name of the function and the names of input and output arguments. For example, this Simulink Function block and Function Caller block both use the argument names u and y.

When calling a function from a Stateflow transition or state label, you can use different argument names. For example, this Simulink Function block uses x and y arguments while the Stateflow transition uses x2 and y2 arguments to call the function.

Function-Call Subsystems Versus Simulink Function Blocks

In general, a Function-Call Subsystem block provides better signal traceability with direct signal connections than a Simulink Function block, whereas a Simulink Function block eliminates the need for routing input and output signal lines through the model hierarchy.

AttributeFunction-Call Subsystem blockSimulink Function block
Method of executing/invoking functionTriggered using a signal lineCalled by reference using the function name
Formal input arguments (Argument Inport blocks) and output arguments (Argument Outport blocks)NoYes
Local inputs (Inport block) and outputs (Outport block)YesYes

Ports

Input

expand all

Placing an Inport block in a subsystem block adds an external input port to the block. The port label matches the name of the Inport block.

Use Inport blocks to receive signals from the local environment.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus | image | struct

An Argument Inport block in a subsystem block provides an input port corresponding to an input argument. A port is not displayed on the subsystem block.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus | image | struct

Output

expand all

Placing an Outport block in a subsystem block adds an output port from the block. The port label on the subsystem block is the name of the Outport block.

Use Outport blocks to send signals to the local environment.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus | image | struct

An Argument Outport block in a subsystem block provides an output port corresponding to an output argument. A port is not displayed on the subsystem block.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus | image | struct

Parameters

expand all

Main

Select how to display port labels on the Subsystem block icon.

  • none — Do not display port labels.

  • FromPortIcon — If the corresponding port icon displays a signal name, display the signal name on the Subsystem block. Otherwise, display the port block name or the port number if the block name is a default name.

  • FromPortBlockName — Display the name of the corresponding port block on the Subsystem block.

  • SignalName — If the signal connected to the port is named, display the name of the signal on the Subsystem block. Otherwise, display the name of the corresponding port block.

For port label editing on Subsystem blocks, see Edit Port Labels on Subsystem Blocks.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ShowPortLabels
Values: 'FromPortIcon' (default) | 'FromPortBlockName' | 'SignalName'

Control user access to the contents of the subsystem.

  • ReadWrite — Enable opening and modification of subsystem contents.

  • ReadOnly — Enable opening but not modification of the subsystem. If the subsystem resides in a block library, you can create and open links to the subsystem and can make and modify local copies of the subsystem but cannot change the permissions or modify the contents of the original library instance.

  • NoReadOrWrite — Disable opening or modification of subsystem. If the subsystem resides in a library, you can create links to the subsystem in a model but cannot open, modify, change permissions, or create local copies of the subsystem.

You do not receive a response if you attempt to view the contents of a subsystem whose Read/Write permissions parameter is set to NoReadOrWrite. For example, when double-clicking such a subsystem, the software does not open the subsystem and does not display any messages.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: Permissions
Values: 'ReadWrite' (default) | 'ReadOnly' | 'NoReadOrWrite'

Enter the name of a function to be called if an error occurs while the software executes the subsystem.

The software passes two arguments to the function: the handle of the subsystem and a character vector that specifies the error type. If no function is specified, the software displays a generic error message if executing the subsystem causes an error.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ErrorFcn
Values: '' (default) | function name in quotes
Data Types: char | string

Select whether to resolve names of workspace variables referenced by this subsystem.

For more information, see Symbol Resolution and Symbol Resolution Process.

  • All — Resolve all names of workspace variables used by this subsystem, including those used to specify block parameter values and Simulink data objects (for example, Simulink.Signal objects).

  • ExplicitOnly — Resolve only names of workspace variables used to specify block parameter values, data store memory (where no block exists), signals, and states marked as “must resolve”.

  • None — Do not resolve any workspace variable names.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: PermitHierarchicalResolution
Values: 'All' (default) | 'ExplicitOnly' | 'None'

Block Characteristics

Data Types

Booleana | busa | doublea | enumerateda | fixed pointa | halfa | integera | singlea | stringa

Direct Feedthrough

no

Multidimensional Signals

yesa

Variable-Size Signals

no

Zero-Crossing Detection

no

a Actual data type or capability support depends on block implementation.

Extended Capabilities

Version History

Introduced in R2014b

expand all