| Contents | Index |
| On this page… |
|---|
Task 1: Add a Function to the Chart Task 2: Define the Subsystem Elements of the Simulink Function |
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.
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_1, r_2,..., r_n] = simfcn(a_1, a_2,..., a_n)
where simfcn is the name of your function, a_1, a_2, ..., a_n are formal names for the arguments, and r_1, r_2, ..., r_n are formal names for the return values.
Click outside the function box.
The following example shows a Simulink function that has the name sim_fcn, which takes three arguments (a, b, and c) and returns two values (x and y).

Note You can also create and edit a Simulink function by using API methods. See API Object Reference for more information. |
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.
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 uint8.
Click OK.
Note 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 double. However, an output port can inherit size and data type. For more information, see Best Practices for Using Simulink Functions. |
![]() | Where to Use a Simulink Function | How a Simulink Function Binds to a State | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |