Call a Simulink Function from a Model - MATLAB & Simulink (original) (raw)

You can use a function caller and the function prototype to call and execute your Simulink® function in a model hierarchy. When a function is called, the caller sends data through input arguments to the function, and then receives data back from the function through output arguments. You can call a Simulink function in several ways.

The following sections show how to call a Simulink function using a Function Caller block, aChart block, and a MATLAB Function block. 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 define the functions, see Define a Simulink Function in a Model.

The model named ex_simulink_functions_and_function_callers shows multiple ways to define and call Simulink® functions.

The model defines Simulink functions in the following ways:

The model calls each of the Simulink functions in the following ways:

Set up a Function Caller block to send data through an input argument to a Simulink function, 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 block parameters. Set theFunction prototype parameter toy = timestwo(x). This function prototype creates an input port x 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.
  3. Add and define a Simulink Function block as described in Define Simulink Function Using Simulink Function Block.
    Note
    The function name and argument names for the Simulink Function block and the Function prototype parameter of 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. Simulate the model. The input sine wave with an amplitude of2 is doubled.

Set up a MATLAB Function block to send data through an input argument to a Simulink function, 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 to open 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 theMATLAB Function block do not have to match the argument names for the function that you define with aSimulink Function block. For aFunction Caller block that calls aSimulink 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 inDefine 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. Set these parameters:
    • Set the Sample time of theSine Wave block to0.01.
    • In the Solver pane of the Configuration Parameters, setType toFixed-step andFixed-step size to0.01.
  3. Simulate the model.

Set up a Stateflow chart to send data through an input argument to a Simulink function, and receive data back from the function through an output argument.

For functions that are defined in referenced models, wrap a Function Caller block in a Simulink function inside the chart.

  1. Add a Stateflow chart to your Simulink model.
  2. Open the chart by double-clicking the block.
  3. From the left-side toolbar, click and drag the default transition icon onto the chart.
  4. Add an input port to the chart.
    1. Open the Model Explorer.
    2. In the left pane, selectChart.
    3. From the menu, select > .
    4. Set Name tox1 andScope 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.
  5. Add an output port to the chart.
    1. From the menu, select > .
    2. Set Name toy1 andScope toOutput.
  6. Enter a label for the transition to call a function. For example, to call the Simulink Function block, enter:
    Note
    Input signals to a Stateflow chart can be continuous or discrete.
  7. Add and setup a Simulink Function block as described inDefine 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. Set these parameters:
    • Set the Sample time of theSine Wave block to0.01.
    • In the Solver pane of the Configuration Parameters, setType toFixed-step andFixed-step size to0.01.
  3. Simulate the model.

You can call a Simulink function multiple times. If there are multiple calls to a function defined in a Simulink Function block, the state values are persistent between the calls originating from different callers.

For example, suppose you have a Stateflow chart with two calls and two Function Caller blocks with calls to the same function,counter.

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

The function named counter is defined in a Simulink Function block. Each time the counter function is called, the input argument, u, is incremented by1.

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 model initialization, each time the function is called, the state value is updated.

In this example, the output observed in Scope1 increments by4 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

When a function is called by multiple callers that have different sample times, data integrity and consistency of real-time code may be an issue. Consider controlling the severity of diagnostics.

Select a Fixed-step solver. Use theTreat each discrete rate as a separate task parameter to specify whether to execute with single task or multitask data transfer.

See Also

Function Caller | Chart (Stateflow) | MATLAB Function | Simulink Function

Topics