Main Content

Define a Simulink Function in a Model

You can define and implement a Simulink® function in several ways.

The following sections show how to define 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.

Define and Call Simulink Functions

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:

  • 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 the following ways:

  • Function Caller block

  • MATLAB Function block

  • Stateflow chart

Define 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 return the output through an output argument.

  1. Add a Simulink Function block to your model.

  2. Double-click the text displayed on the block and enter the function prototype.

    y = timestwo(x)

    After you configure the block, the block icon displays the function prototype for the Simulink Function.

  3. Double-click the block to open the subsystem.

  4. Add a Gain block and set the Gain parameter to 2.

    To represent the function algorithm, the Simulink canvas has a trigger port, f, with an input of x, gain block with value 2, and an output of y.

Define Simulink Function Using Exported Function from Stateflow Chart

You can define a Simulink function using a function exported from a Stateflow chart. In a Stateflow chart, you can implement function behavior using a graphical function, MATLAB function, or Simulink function. This section shows how to define and export a graphical function. You can also follow these steps to define and export a MATLAB function or Simulink function.

Set up a graphical function in a Stateflow chart to receive data through an input argument from a function caller and then return the output through an output argument. Set chart parameters to export the function to a Simulink model.

Define Graphical Function in Stateflow Chart

Define a graphical function in a Stateflow chart. Specify the function prototype and implement function behavior.

  1. Add a Stateflow Chart to your Simulink model.

  2. Double-click to open the chart.

  3. Add a graphical function. From the left-side toolbar, click and drag the graphical function icon onto the chart.

  4. Specify the function prototype. In the function box, replace the ? with the function prototype y = timestwo(x).

  5. Implement function behavior. Click the transition arrow and replace the ? with {y = 2*x}.

    After you configure the graphical function, the block icon displays the function prototype and the function definition.

Test Graphical Function

Test the graphical function within the Stateflow chart before exporting to a Simulink model.

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

  2. Double-click the arrow and replace the ? with {y1 = timestwo(x1)}.

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

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

  5. 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 graphical function, there is a Sine Wave block as input to a Stateflow chart which sends output to a Scope block.

  6. Simulate the chart.

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.

  1. Open the Model Explorer. On the Modeling tab and from the Design section, select Model Explorer .

  2. In the left pane, select the graphical function.

    Modeling Hierarchy pane of the Model Explorer shows the timestwo graphical function under the Chart.

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

    Column View pane of the Model Explorer shows the arguments of the timestwo graphical function.

  4. In the right pane, set Size to 1, set Complexity to Off, and set Type to double.

    Data pane of the Model Explorer shows the parameters set for x.

  5. Repeat steps 2 and 3 with the output function argument y.

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.

  1. Open the Model Explorer. In the left pane, select the chart containing the graphical function.

    Modeling Hierarchy pane of the Model Explorer shows the timestwo graphical function under the Chart.

  2. In the Property Inspector, select Export chart level functions and click the Apply button. Then select Treat exported functions as globally visible.

    If you are calling the exported graphical function from another Stateflow chart, you do not need to enable the Treat exported functions as globally visible parameter.

For more information, see Export Stateflow Functions for Reuse (Stateflow).

See Also

| (Stateflow) | | |

Related Topics