| Contents | Index |
A graphical function in a Stateflow chart is a graphical element that helps you reuse control-flow logic and iterative loops. This function is a program you write with flow graphs using connective junctions and transitions. You create a graphical function, fill it with a flow graph, and call the function in the actions of states and transitions.
This function helps you to:
Create modular, reusable logic that you can call anywhere in your chart.
Track simulation behavior visually during chart animation.
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. Follow these guidelines:
If you want to call the function only within one state or subchart and its substates, put your graphical function in that state or subchart. That function overrides any other functions of the same name in the parents and ancestors of that state or subchart.
If you want to call the function anywhere in that chart, put your graphical function at the chart level.
If you want to call the function from any chart in your model, put your graphical function at the chart level and enable exporting of chart-level graphical functions. For instructions, see Exporting Chart-Level Graphical Functions.
Use these steps to create a graphical function in your chart:
Click the graphical function icon in the 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 In the chart, you can 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 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:
Open the Model Explorer.
Expand the chart 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
.
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.
Tip If the formal arguments of a function signature are scalars, verify that inputs and outputs of function calls follow the rules of scalar expansion. For more information, see How Scalar Expansion Works for Functions. |
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.
To export graphical functions to your main model:
Open the chart where your graphical function resides.
Open 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 unsupported
inputs or outputs
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 in the library charts:
For lib1Chart, add this graphical function.

For lib2Chart, add this graphical function.

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

For each chart, follow these steps:
Open the Chart properties dialog box.
In the Chart properties dialog box, select Export Chart Level Graphical Functions (Make Global).
Click OK.
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.
Open the Model Explorer.
In the Model Hierarchy pane of the Model Explorer, navigate to main_model.
Add the data x and y to the Stateflow machine:
Select Add > Data.
In the Name column, enter x.
In the Initial Value column, enter 0.
Use the default settings for other properties of x.
Select Add > Data.
In the Name column, enter y.
In the Initial Value column, enter 1.
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.
Open the Configuration Parameters dialog box.
In the Configuration Parameters dialog box, go to the Solver pane.
In the Solver options section, make these changes:
For Type, select Fixed-step.
For Solver, select Discrete (no continuous states).
For Fixed-step size, enter 1.
Click OK.
This step ensures that when you simulate your model, a discrete solver is used. For more information, see Solvers in the Simulink 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:
Open the Simulink Library Browser.
From the Simulink/Sinks Library, select the Scope block and add it to main_model.
Open the Model Explorer.
In the Model Hierarchy pane, navigate to modChart.
Add the output data z to the chart:
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 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. |
![]() | Maintaining Transition Shapes with Smart Behavior | Grouping Chart Objects with Boxes | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |