Main Content

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.

  1. Add a Simulink Function block to your model.

  2. On the block face, 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 defining the function algorithm.

  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.

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.

  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 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. Define the function interface. In the function box, replace the ? with the function interface y = timestwo(x).

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

    After you configure the graphical function, the block icon displays the function interface and the function algorithm.

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

  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 (scalar), Set Complexity to Off (real number), 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.

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

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

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

  2. Open the chart.

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

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

    MATLAB Function called timestwo.

  5. Double-click the function box to open the MATLAB code editor. Define the function algorithm with the MATLAB code.

    MATLAB code editor with function algorithm defined as y equals 2 times x.

Test MATLAB Function

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

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

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

  2. In the left pane, select the MATLAB 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.

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

  4. In the right pane, set Size to 1 (scalar), Set Complexity to Off (real number), and set Type to double.

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

Set Export Function Parameters for MATLAB Function

Set parameters to export a MATLAB function from a Stateflow chart during a simulation.

  1. Open the Model Explorer.

  2. From the left pane, select the chart containing the MATLAB function.

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

| | | |

Related Topics