A Simulink® function is a graphical object that you fill with Simulink blocks and call in the actions of states and transitions. Simulink functions are supported only in Stateflow® charts in Simulink models. For more information, see Reuse Simulink Components in Stateflow Charts.
Follow these steps to add a Simulink function to the chart:
Click the Simulink function icon in the Stateflow Editor toolbar:
Move your pointer to the location for the new Simulink function in your chart and click to insert the function box.
You can also drag the function from the toolbar.
Enter the function signature.
The function signature specifies a name for your function and the formal names for the arguments and return values. A signature has this syntax:
r_n] = simfcn(
simfcn is the name of your function,
a_n are formal names for the arguments, and
r_n are formal names for the return values.
This syntax is the same as what you use for graphical functions, truth tables, and MATLAB® functions. You can define arguments and return values as scalars, vectors, or matrices of any data type.
Click outside the function box.
The following example shows a Simulink function that has the name
sim_fcn, which takes three
c) and returns
two values (
You can also create and edit a Simulink function by using API methods.
Follow these steps to define the subsystem elements of the Simulink function:
Double-click the Simulink function box.
The contents of the subsystem appear: input and output ports that match the function signature and a single function-call trigger port.
Add Simulink blocks to the subsystem.
Connect the input and output ports to each block.
You cannot delete the trigger port in the function.
The following example shows the subsystem elements for a Simulink function.
Follow these steps to configure inputs for the Simulink function:
Configure the input ports.
Double-click an input port to open the Block Parameters dialog box.
In the Signal Attributes pane, enter the size and data type.
For example, you can specify a size of
[2 3] for a 2-by-3
matrix and a data type of
An input port of a Simulink function cannot inherit size or data type. Therefore, you define the size
and data type of an input that is not scalar data of type
However, an output port can inherit size and data type.