Call a Simulink Function from a Model
Simulink® functions have an interface with input and output arguments similar to programming languages. Simulink function callers send data through input arguments to Simulink functions, execute the function, and then receive data back from the function through output arguments. You can call a Simulink function using:
Function Caller blocks
MATLAB Function blocks
Stateflow® charts
The following sections show how to call a Simulink function. The function y = timestwo(x)
multiplies a value
(x
) from a caller by 2
, and then sends the calculated
value (y
) back to the caller. To create the functions, see Add a Simulink Function to 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.
Use a Function Caller Block to Call a Simulink Function Block
Set up a Function Caller block to send data through an input argument to a Simulink Function block, and receive data back from the function through an output argument.
Add a Function Caller block to your model.
Open the Function Caller dialog box. In the Function prototype box, enter
y = timestwo(x)
. This function prototype creates an input portx
and output porty
on the Function Caller block.Note
Typing in a blank text box displays a list of previously created function prototypes that match the text you are typing.
Add and setup a Simulink Function block as described in Create Simulink Function Using Simulink Function Block.
Note
The function and argument names for the Simulink Function block and the Function prototype for the Function Caller block must match exactly.
Test the function call
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. The input sine wave with an amplitude of
2
is doubled.
Use a MATLAB Function Block to Call a Simulink Function Block
Set up a MATLAB Function block to send data through an input argument to a Simulink Function block, and receive data back from the function through an output argument.
Add a MATLAB Function block to your model.
Double-click the block, which opens the MATLAB® editor. Enter the function call
y1 = timestwo(x1)
.Note
The argument names for the function you define in the MATLAB Function block do not have to match the argument names for the function that you define with a Simulink Function block. For a Function Caller block that calls a Simulink Function block, argument names must match.
Note
MATLAB Function blocks only support discrete and fixed-in-minor sample times.
Add and setup a Simulink Function block as described in Create Simulink Function Using Simulink Function Block.
Test the function call
Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.
For the Sine Wave block, set the Sample time to
0.01
. For the model, open the Configuration Parameters dialog box to the solver pane. Set Type toFixed-step
and Fixed-step size to0.01
.Run a simulation.
Use a Stateflow Chart to Call a Simulink Function Block
Set up a Stateflow chart to send data through an input argument to a Simulink Function block, and receive data back from the function through an output argument.
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 the chart to open it.
From the left-side toolbar, click and drag the default transition icon onto the chart.
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
.Note
The argument names for the function you define in the Stateflow chart do not have to match the argument names for the function that you define with a Simulink Function block. For a Function Caller block that calls a Simulink Function block, argument names must match.
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 and connect signal output to the chart input port. Add a Scope block and connect input to the chart output port.
Edit transition code to call a function. For example, to call the Simulink Function block, enter:
{y1=timestwo_sf(x1);}
Note
Input signals to a Stateflow chart can be either continuous or discrete.
Add and setup a Simulink Function block as described in Create Simulink Function Using Simulink Function Block.
Test the function call
Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.
For the Sine Wave block, set the Sample time to
0.01
. For the model, open the Configuration Parameters dialog box to the solver pane. Set Type toFixed-step
and Fixed-step size to0.01
.Run a simulation.
Call a Simulink Function Block from Multiple Sites
If you call a Simulink Function block from multiple sites, all call sites share the state of the function. For example, suppose that you have a Stateflow chart with two calls and two Function Caller blocks with calls to the same function.
A function defined with a Simulink Function block is a counter that
increments by 1
each time it is called with an input of
1
.
The Unit Delay block has state because the block value is persistent between calls from the two Function Caller blocks and the Stateflow chart. Conceptually, you can think of this function being implemented in MATLAB code:
function y = counter(u) persistent state; if isempty(state) state = 0; end y = state; state = state + u;
Simulink initializes the state value of the Unit Delay block at the beginning of a simulation. After that, each time the function is called, the state value is updated.
In this example, the output observed in Scope1
increments by
4
at each time step. Scope2
,
Scope3
, and Scope4
show a similar behavior. The only
difference is a shift in the observed signal due to the execution sequence of the function
calls.
Diagnostic Settings With Multiple Callers
For multiple callers that share a function and have different sample time rates, data integrity and consistency of real-time code might be a problem. Consider controlling the severity of diagnostics.
Select a Fixed-step
solver. Set the Treat each
discrete rate as a separate task parameter to:
Clear (single-tasking), and then set the Single task data transfer parameter to
none
(default),warning
, orerror
.Select (multi-tasking), and then set the Multitask data transfer parameter to
error
(default) orwarning
.
See Also
Simulink Function | Argument Inport | Argument Outport | Function Caller | MATLAB Function