Main Content

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.

  1. Add a Function Caller block to your model.

  2. Open the Function Caller dialog box. In the Function prototype box, enter y = timestwo(x). This function prototype creates an input port x and output port y 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.

  3. 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.

    After you configure the block, the Function Caller block has an input port x and an output port y. The function prototype is displayed in the Simulink Function block.

Test the function call

  1. Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.

    After you add the components to test the function call, there is a Sine Wave block as input to the function caller which sends output to a Scope block.

  2. Run a simulation. The input sine wave with an amplitude of 2 is doubled.

    A scope displaying a sine wave with an amplitude of 4.

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.

  1. Add a MATLAB Function block to your model.

  2. Double-click the block, which opens the MATLAB® editor. Enter the function call y1 = timestwo(x1).

    MATLAB code editor with function algorithm defined as y1 equals timestwo of 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.

  3. Add and setup a Simulink Function block as described in Create Simulink Function Using Simulink Function Block.

    After you configure the block, the Function Caller block has an input port x1 and an output port y1. The function prototype is displayed in the Simulink Function block.

Test the function call

  1. Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.

    After you add the components to test the function call, there is a Sine Wave block as input to the function caller which sends output to a Scope block.

  2. 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 to Fixed-step and Fixed-step size to 0.01.

  3. 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.

  1. 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, select Chart.

  2. Double-click the chart to open it.

  3. From the left-side toolbar, click and drag the default transition icon onto the chart.

  4. 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 to x1 and Scope to Input.

    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.

  5. Add an output port to the chart. From the menu, select Add > Data. Set Name to y1 and Scope to Output.

  6. 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.

  7. 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.

  8. Add and setup a Simulink Function block as described in Create Simulink Function Using Simulink Function Block.

    After you configure the Stateflow chart, the chart block has an input of x1, output of y1, and shows the transition code which calls the Simulink Function.

Test the function call

  1. Add a Sine Wave block to provide test data for the input and a Scope block to view results from the output.

    After you add the components to test the function call, there is a Sine Wave block as input to the Stateflow chart which sends output to a Scope block.

  2. 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 to Fixed-step and Fixed-step size to 0.01.

  3. 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.

Simulink canvas with a Stateflow chart, a Simulink Function block, and 2 Function Caller blocks.

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.

To represent the function algorithm, the Simulink canvas has a trigger port, counter, with an input of u, an Add block, a Unit Delay block, and an output of y.

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.

A scope displaying a step function incrementing by 4 each time step.

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, or error.

  • Select (multi-tasking), and then set the Multitask data transfer parameter to error (default) or warning.

See Also

| | | |

Related Topics