Add a Simulink Function to a Model
Simulink® functions have an interface with input and output arguments similar to programming languages. You can create the function definition for a Simulink function using:
Simulink blocks within a Simulink Function block.
Stateflow® state transitions in a graphical function exported from a Stateflow chart.
MATLAB® code in a MATLAB function exported from a Stateflow chart.
The following sections show how to create a Simulink function for the function y = timestwo(x)
. The function
multiplies a value (x
) from a caller by 2
, and then
sends the calculated value (y
) back to the caller. To call the function,
see Call a Simulink Function from a Model.
Simulink Functions and Function Callers
The model named ex_simulink_functions_and_function_callers
shows multiple ways to create and call Simulink functions.
The model creates Simulink functions in three ways:
Simulink Function block
Stateflow chart that exports a graphical function
Stateflow chart that exports a MATLAB function
The model calls each of the Simulink functions in three ways:
Function Caller block
MATLAB Function block
Stateflow chart
To visually display the connections between the Simulink functions and their callers with lines, on the Debug tab, under Information Overlays, the Function Connectors button is selected.
Create Simulink Function Using Simulink Function Block
Set up a Simulink Function block to receive data through an input argument from a function caller, and then pass a calculated value back through an output argument.
Add a Simulink Function block to your model.
On the block face, enter the function prototype.
y = timestwo(x)
Double-click the block to open the subsystem defining the function algorithm.
Add a Gain block and set the Gain parameter to
2
.
Create Simulink Function Using Exported Graphical Function from Stateflow Chart
Set up a graphical function in a Stateflow chart to receive data through an input argument from a function caller and pass the calculated value back through an output argument. Set chart parameters to export the function to a Simulink model.
Define Graphical Function in Stateflow Chart
Create a graphical function in a Stateflow chart. Define the function interface and function definition.
Add a Stateflow Chart to your Simulink model. Double-click on the Simulink block diagram. In the search box, enter
chart
, and then from the search results, selectChart
.Double-click to open the chart.
Add a graphical function. From the left-side toolbar, click and drag the graphical function icon onto the chart.
Define the function interface. In the function box, replace the
?
with the function interfacey = timestwo(x)
.Define the function algorithm. Click the transition arrow and replace the
?
with{y = 2*x}
.
Test Graphical Function
Test the graphical function within the Stateflow chart before exporting to a Simulink model.
Add a default transition in the chart for testing the function. From the left-side toolbar, click and drag a default transition arrow onto the chart.
Double-click the arrow and replace the
?
with{y1 = timestwo(x1)}
.Add an input port to the chart. Open the Model Explorer. In the left pane, select
Chart
. From the menu, select Add > Data. Set Name tox1
and Scope toInput
.Add an output port to the chart. From the menu, select Add > Data. Set Name to
y1
and Scope toOutput
.Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.
Run a simulation.
Set Argument Parameters for Graphical Function
Specify the size, complexity, and type of the function input and output arguments. A chart can export only functions with fully specified prototypes.
Open the Model Explorer. On the Modeling tab and from the Design section, select Model Explorer .
In the left pane, select the graphical function.
From the Column View list in the middle pane, select
Stateflow
. Select the filter icon , and then from the toggle list, select All Stateflow Objects. From the center pane table, select an input or output argument.In the right pane, set Size to 1 (scalar), Set Complexity to
Off
(real number), and set Type todouble
.Repeat steps
2
and3
with the output function argumenty
.
Set Export Function Parameters for Graphical Function
Set parameters to export a graphical function to a Simulink model from a Stateflow chart during a simulation.
Open the Model Explorer.
In the left pane, select the chart containing the graphical function.
In the property dialog box on the right side, select the Export Chart Level Functions check box, click the Apply button, and then select the Treat Exported Functions as Globally Visible check box.
If you are calling the exported graphical function from another Stateflow chart (not the chart that exported the graphical function), you do not need to select the Treat Exported Functions as Globally Visible check box.
Create Simulink Function Using Exported MATLAB Function from Stateflow Chart
Set up a MATLAB function in a Stateflow chart to receive data through an input argument from a function caller and then pass a calculated value back through an output argument. Set chart parameters to export the function to a Simulink model.
Define MATLAB Function in Stateflow Chart
Create a MATLAB function in a Stateflow chart. Define the function interface and function definition.
Add a Stateflow Chart to your Simulink model. Double-click on the block diagram. In the search box, enter
chart
, and then from the search results, selectChart
.Open the chart.
Add a MATLAB function. From the left-side toolbar, click and drag the graphical function icon onto the chart.
Define the function interface. In the function box, replace the
?
with the function interfacey = timestwo(x)
.Double-click the function box to open the MATLAB code editor. Define the function algorithm with the MATLAB code.
Test MATLAB Function
Test the MATLAB function within a Stateflow chart before exporting to a Simulink model.
Add a default transition in the chart for testing the function. From the left-side toolbar, click and drag a default transition arrow onto the chart.
Double-click the arrow and replace the
?
with{y1 = timestwo(x1)}
.Add an input port to the chart. Open the Model Explorer. In the left pane, select
Chart
. From the menu, select Add > Data. Set Name tox1
and Scope toInput
.Add an output port to the chart. From the menu, select Add > Data. Set Name to
y1
and Scope toOutput
.Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.
Run a simulation.
Set Argument Parameters for MATLAB Function
Specify the size, complexity, and type of the function input and output arguments. A chart can export only functions with fully specified prototypes.
Open the Model Explorer. On the Modeling tab and from the Design section, select Model Explorer .
In the left pane, select the MATLAB function.
From the Column View list in the middle pane, select
Stateflow
. Select the filter icon , and then from the toggle list, select All Stateflow Objects. From the center pane table, select an input or output argument.In the right pane, set Size to 1 (scalar), Set Complexity to
Off
(real number), and set Type todouble
.Repeat steps
2
and3
with the output function argumenty
.
Set Export Function Parameters for MATLAB Function
Set parameters to export a MATLAB function from a Stateflow chart during a simulation.
Open the Model Explorer.
From the left pane, select the chart containing the MATLAB function.
In the property dialog box on the right side, select the Export Chart Level Functions check box, click the Apply button, and then select the Treat Exported Functions as Globally Visible check box.
If you are calling the exported MATLAB function from another Stateflow chart (not the chart that exported the MATLAB function), you do not need to select the Treat Exported Functions as Globally Visible check box.
See Also
Simulink Function | Argument Inport | Argument Outport | Function Caller | MATLAB Function