Main Content

Export Stateflow Functions for Reuse

When you export chart-level functions, you extend the scope of your functions to other blocks in the Simulink® model. For an example, see Share Functions Across Simulink and Stateflow. You can export these functions:

  • Graphical functions

  • MATLAB® functions

  • Truth tables

How to Export Chart-Level Functions

  1. Open the chart where your function resides.

  2. In the Property Inspector, open the Advanced section.

  3. Select Export Chart Level Functions.

  4. If your function resides in a library chart, link that chart to your main model.

When you select Export Chart Level Functions, you can call exported functions by using Simulink Caller blocks with dot notation, chartName.functionName. To call the exported functions throughout the model from any Stateflow® or Simulink Caller block, select Treat Exported Functions as Globally Visible. Do not use dot notation to call these functions. You cannot export functions with the same name.

Simulink functions can also be defined directly in the Simulink canvas. For more information, see Simulink Function.

Rules for Exporting Chart-Level Functions

 Link library charts to your main model to export chart-level functions from libraries

 Do not export chart-level functions that contain unsupported inputs or outputs

 Do not export Simulink functions

 Do not export functions across model reference boundaries

Export Chart-Level Functions

This example describes how to export functions in library charts to your main model.

  1. Create these objects:

    • Add a model named main_model, with a chart named modChart.

    • Add a library model named lib1, with a chart named lib1Chart.

    • Add a library model named lib2, with a chart named lib2Chart.

  2. Create these graphical functions in the library charts:

    • For lib1Chart, add this graphical function.

    • For lib2Chart, add this graphical function.

    • In the Model Explorer, for each of the function inputs and outputs, a, b, and c, set these properties:.

      • Size to 1

      • Complexity to Off

      • Type to double

  3. For modChart, add a graphical function and a default transition with a lib1_func action.

  4. For each chart, follow these steps:

    1. In the Model Explorer, for each of the function inputs and outputs, a, b, and c, set:

      • Size to 1

      • Complexity to Off

      • Type to double

    2. Open the Chart properties dialog box.

    3. In the Chart properties dialog box, select Export chart level functions and Treat exported functions as globally visible.

    4. Click OK.

  5. Drag lib1Chart and lib2Chart into main_model from lib1 and lib2, respectively. Your main model should look something like this:

    Each chart now defines a graphical function that any chart in main_model can call.

  6. Open the Model Explorer.

  7. In the Model Hierarchy pane of the Model Explorer, navigate to main_model.

  8. Add the data x and y to the Stateflow machine:

    1. Select Add > Data.

    2. In the Name column, enter x.

    3. In the Initial Value column, enter 0.

    4. Use the default settings for other properties of x.

    5. Select Add > Data.

    6. In the Name column, enter y.

    7. In the Initial Value column, enter 1.

    8. Use the default settings for other properties of y.

    This step ensures that input and output data are defined globally to support exported graphical functions.

  9. Open the Model Configuration Parameters dialog box.

  10. In the Model Configuration Parameters dialog box, go to the Solver pane.

  11. In the Solver selection section, make these changes:

    1. For Type, select Fixed-step.

    2. For Solver, select Discrete (no continuous states).

  12. In the Solver details section, make these changes:

    1. For Fixed-step size, enter 1.

    2. Click OK.

    This step ensures that when you simulate your model, a discrete solver is used. For more information, see Compare Solvers.

What Happens During Simulation

When you simulate the model, these actions take place during each time step.

PhaseThe object...Calls the graphical function...Which...
1modChartlib1_funcReads two input arguments x and y
2lib1_funclib2_funcPasses the two input arguments
3lib2_funcmod_funcAdds x and y and assigns the sum to x

How to View the Simulation Results

To view the simulation results, add a scope to your model. Follow these steps:

  1. Open the Simulink Library Browser.

  2. From the Simulink/Sinks Library, select the Scope block and add it to main_model.

  3. Open the Model Explorer.

  4. In the Model Hierarchy pane, navigate to modChart.

  5. Add the output data z to the chart:

    1. Select Add > Data.

    2. In the Name column, enter z.

    3. In the Scope column, select Output.

    4. Use the default settings for other properties.

  6. For modChart, update the default transition action to read as follows:

    {x = lib1_func(x,y); z = x;}
  7. In the model, connect the outport from modChart to the inport of the Scope block.

  8. Double-click the Scope block to open the display.

  9. Start simulation.

  10. After the simulation ends, right-click in the scope display and select Autoscale.

    The results look something like this: