Export Stateflow Functions for Reuse
You can extend the scope of the chart-level functions in your Stateflow® chart to other blocks in the Simulink® model by exporting the functions. You can export:
When you export chart-level functions, you can call them in other Stateflow charts and Simulink Caller blocks.
To export chart-level functions that you can call by using qualified notation such as
, select the Export chart level functions chart property, as described in Specify Properties for Stateflow Charts.
To export chart-level functions that you can call without using qualified notation, select Export chart level functions, and then select Treat exported functions as globally visible. You cannot export functions with the same name.
Share Functions Across Stateflow Charts
This example shows how to call exported functions from other charts in your Simulink model. This model contains a main Stateflow chart,
mainChart, and two auxiliary library charts,
lib2Chart. Each chart contains a chart-level graphical function and has both the Export chart level functions and Treat exported functions as globally visible chart properties enabled.
The main chart contains two data objects,
y, with initial values of
1, respectively. When you simulate the model, the default transition in this chart calls the function
lib1_func using these values as arguments.
lib1_func is defined in the library chart
lib1Chart. The function reads its input arguments and passes them to the function
lib2_func is defined in the library chart
lib2Chart. The function reads its input arguments and passes them to the function
main_func is defined in the main chart. The function adds its input arguments and returns the result. The main chart stores this result as the output data
x. The sequence of function calls repeats in each time step of the simulation. The Scope block shows the value of
x increasing during the simulation.
Guidelines for Exporting Chart-Level Functions
Do Not Export Chart-Level Functions That Contain Unsupported Inputs or Outputs
You cannot export a chart-level function when inputs or outputs have any of the following properties:
Fixed-point data type with word length greater than 32 bits
Do Not Export Simulink Functions
If you enable the Export chart level functions chart property in a chart that contains chart-level Simulink functions, a run-time error occurs. To resolve the error, disable the Export chart level functions chart property or move the Simulink functions to a lower level in the chart hierarchy.
Alternatively, you can call a Simulink function from other blocks in the model by defining it directly in the Simulink canvas using a Simulink Function (Simulink) block. For more information, see Share Functions Across Simulink and Stateflow.
Do Not Export Functions Across Model Reference Boundaries
You cannot export functions from a referenced model and call the functions from a parent model.
Combine Output and Update Functions When Generating Code
If you generate code for a model that uses exported chart-level functions, enable the model configuration parameter Single output/update function (Simulink Coder) to ensure consistent behavior between simulation and code generation.