| Stateflow® | ![]() |
| On this page… |
|---|
Where to Use a Graphical Function Workflow for Defining a Graphical Function Managing Large Graphical Functions Calling Graphical Functions in Stateflow Action Language |
A graphical function is a program that you write with flow graphs using connective junctions and transitions. You create a graphical function, fill it with a flow graph, and call it many times in the actions of states and transitions.
A graphical function is easier to create, access, and manage than a textual function, such as a C or MATLAB function that you must define externally. Like a textual function, a graphical function can accept arguments and return values. Unlike a textual function, a graphical function is a native Stateflow object. You use the Stateflow Editor to create a graphical function that resides in your model along with the charts that invoke the function.
A graphical function can reside anywhere in a chart, state, or subchart. The location of a function determines its scope, that is, the set of states and transitions that can call the function. In particular, graphical functions are visible to the chart, to the parent state and its parents, and to sibling transitions and states. These exceptions apply:
If the chart containing the function exports its graphical functions, the scope of the function is the entire Stateflow machine, which encompasses all the charts in the model. See Exporting Chart-Level Graphical Functions for more information.
A function that you define in a state or subchart overrides any functions of the same name in the parents and ancestors of that state or subchart.
Use these steps to create a graphical function in your chart:
Click the graphical function icon in the Stateflow Editor toolbar:
![]()
Move your pointer to the location for the new graphical function in your chart and click to insert the function box.
The function signature specifies a name for your function and the formal names for its arguments and return values. A signature has this syntax:
[r1, r2,..., rn] = func(a1,a2,..., an)
where func is the name of your function, a1, a2, ..., an are formal names for its arguments, and r1, r2, ..., rn are formal names for its return values.
Click outside of the function box.
The following signature is for a graphical function that has the name f1, which takes three arguments (a, b, and c) and returns three values (x, y, and z).

Note You can use the Stateflow Editor to change the signature of your graphical function at any time. After you edit the signature, the Model Explorer updates to reflect the changes. |
To program a graphical function, follow these steps:
Click the default transition icon in the Stateflow Editor toolbar:
![]()
Move your pointer inside the function box in your chart and click to insert the default transition and its terminating junction.
Enter transition conditions and actions for your graphical function. If necessary, add connective junctions and transitions to your function.
This function box shows a flow graph that returns different products of its arguments.

You must define the data in your graphical function:
In the Stateflow Editor, select View > Model Explorer.
The signature for your function appears as a property of its parent chart in the Contents pane of the Model Explorer.

Expand the parent object in the Model Explorer, so that you can see the return values and arguments of the function signature as data items that belong to your graphical function.

The Scope column in the Model Explorer indicates the role of each argument or return value. Arguments have the scope Input, and return values have the scope Output.
For each function argument and return value, right-click the data row in the Model Explorer and select Properties from the context menu.
In the Data properties dialog box for each argument and return value, specify the data properties.
These rules apply:
Each argument and return value can be a scalar or matrix of values.
Arguments cannot have initial values.
Create any additional data items that your function must have to process its programming.
Your function can access its own data or data belonging to parent states or the chart. The data items that you create for the function itself can have one of these scopes:
Local
Local data persists from one function call to the next.
Temporary
Temporary data initializes at the start of every function call.
Constant
Constant data retains its initial value through all function calls.
You can make your graphical function as large as you want, as shown below.

However, if your function grows too large, you can hide its contents by right-clicking inside the function box and selecting Make Contents > Subcharted from the context menu. This option makes your graphical function opaque.

To access the programming of your subcharted graphical function, double-click it. This action dedicates the entire chart window to programming your function.

To access your original chart, click the Back button
.
To call your graphical function, use Stateflow action language. Any state or transition action in the scope of your function can perform a function call.
Syntax for a function call is the same as that of a function signature, with actual arguments replacing the formal ones specified in a signature. If the data types of the actual and formal argument differ, a function casts the actual argument to the type of the formal argument.
See Creating a Graphical Function for information about syntax for a function signature.
In this example, a state entry action calls a graphical function that returns three products.

When you export chart-level graphical functions, you extend the scope of your functions to all other charts in your model.
Perform these steps to export graphical functions to your main model:
Open the chart where your graphical function resides.
In the Stateflow Editor, select File > Chart Properties.
In the Chart properties dialog box, select Export Chart Level Graphical Functions (Make Global).
If your graphical function resides in a library chart, link that chart to your main model.
Link library charts to your main model to export graphical
functions from libraries
Do not export graphical functions that contain inputs or outputs
of enumerated data type
This example describes how to export graphical functions in library charts to your main model.
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.

Create these graphical functions:
For modChart, add this graphical function.

For lib1Chart, add this graphical function.

For lib2Chart, add this graphical function.

For modChart, add a default transition with this action.

For each chart, follow these steps:
Select File > Chart Properties in the Stateflow Editor.
In the Chart properties dialog box, select the Export Chart Level Graphical Functions (Make Global) option.
Click OK.
Drag lib1Chart and lib2Chart into main_model from lib1 and lib2, respectively.

Each chart now defines a graphical function that any chart in main_model can call.
In the Stateflow Editor, select View > Model Explorer.
In the Model Hierarchy pane of the Model Explorer, navigate to main_model.
Add the data x to the Stateflow machine as follows:
Select Add > Data.
In the Name column, enter x.
In the Initial Value column, enter 0.
Use the default settings for other properties.
Add the data y to the Stateflow machine as follows:
Select Add > Data.
In the Name column, enter y.
In the Initial Value column, enter 1.
Use the default settings for other properties.
In the Stateflow Editor, select Simulation > Configuration Parameters.
In the Configuration Parameters dialog box, go to the Solver pane.
In the Solver options section, make these changes:
For Type, choose Fixed-step.
For Solver, choose Discrete (no continuous states).
For Fixed-step size, enter 1.
Click OK.
These actions ensure that when you simulate your model, a discrete solver is used. For more information, see Solvers in the Simulink software documentation.
What Happens During Simulation. When you simulate the model, these actions take place during each time step.
| Phase | The object... | Calls the graphical function... | Which... |
|---|---|---|---|
| 1 | modChart | lib1_func | Reads two input arguments x and y |
| 2 | lib1_func | lib2_func | Passes the two input arguments |
| 3 | lib2_func | mod_func | Adds 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:
In the Simulink model window, select View > Library Browser.
From the Simulink/Sinks Library, select the Scope block and add it to the model.
Open the Model Explorer.
In the Model Hierarchy pane, navigate to modChart.
Add the output data z to the Stateflow chart as follows:
Select Add > Data.
In the Name column, enter z.
In the Scope column, select Output.
Use the default settings for other properties.
For modChart, update the default transition action to read as follows:
{x = lib1_func(x,y); z = x;}In the Simulink model window, connect the outport from modChart to the inport of the Scope block.

Double-click the Scope block to open the display.
Start simulation.
After the simulation ends, right-click in the scope display and select Autoscale.
The results look something like this.

You can set general properties for your graphical function through its properties dialog box:
Select Properties from the context menu.
The properties dialog box for your graphical function appears.

The fields in the properties dialog box are:
Field | Description |
|---|---|
Click this read-only function name to bring your function to the foreground in its native chart. | |
Select Function Call to set a breakpoint that pauses simulation when your graphical function executes. | |
Select one of these options to control the inlining of your function in generated code:
| |
Specify the signature label for your function in this field. See Creating a Graphical Function for more information. | |
Enter a textual description or comment. | |
Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit/spec/data/speed.txt. |
![]() | Extending Transitions with Smart Behavior | Using Boxes to Extend Charts | ![]() |
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |