Main Content

Reuse Logic Patterns by Defining Graphical Functions

A graphical function in a Stateflow® chart is a graphical element that helps you reuse control-flow logic and iterative loops. You create graphical functions with flow charts that use connective junctions and transitions. You can call a graphical function in the actions of states and transitions. With graphical functions, you can:

  • 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 the function determines the set of states and transitions that can call the function.

  • If you want to call the function 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 a 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 functions. For more information, see Export Stateflow Functions for Reuse.

Note

A graphical function can access chart and state data above it in the Stateflow hierarchy.

For example, this graphical function has the name f1. It takes three arguments (a, b, and c) and returns three output values (x, y, and z). The function contains a flow chart that computes three different products of the arguments.

A graphical function that computes three different products.

Define a Graphical Function

  1. In the object palette, click the graphical function icon .

  2. On the chart canvas, click the location for the new graphical function.

  3. Enter the signature label for the function.

    The signature label of the function specifies a name for your function and the formal names for its arguments and return values. A signature label has this syntax:

    [return_val1,return_val2,...] = function_name(arg1,arg2,...)
    You can specify multiple return values and multiple input arguments. Each return value and input argument can be a scalar, vector, or matrix of values. For functions with only one return value, omit the brackets in the signature label.

    You can use the same variable name for both arguments and return values. When you use the same variable for an input and output, you create in-place data. For example, a function with this signature label uses the variables y1 and y2 as both inputs and outputs:

    [y1,y2,y3] = f(y1,u,y2)
    If you export this function to C code, the generated code treats y1 and y2 as in-place arguments passed by reference. Using in-place data reduces the number of times that the generated code copies intermediate data, which results in more efficient code.

    In the Symbols pane and the Model Explorer, the arguments and return values of the function signature appear as data items that belong to your function. Arguments have the scope Input. Return values have the scope Output.

  4. Specify the data properties for each argument and return value, as described in Set Data Properties. When an argument and a return value have the same name, you can edit properties only for the argument. The properties for the return value are read-only.

  5. To program the function, construct a flow chart inside the function box, as described in Create Flow Charts in Stateflow.

    Because a graphical function must execute completely when you call it, you cannot use states. Connective junctions and transitions are the only graphical elements that you can use in a graphical function.

    Note

    In a graphical function, do not broadcast events that can cause the active state to change. In a graphical function, the behavior of an event broadcast that causes an exit from the active state is unpredictable.

  6. Create any additional data items required by your function. For more information, see Add Data Through the Model Explorer.

    Your function can access its own data or data belonging to parent states or the chart. The data items in the function can have one of these scopes:

    • Constant — Constant data retains its initial value through all function calls.

    • Parameter — Parameter data retains its initial value through all function calls.

    • Local — Local data persists across function calls throughout the simulation. Valid only in charts that use C as the action language.

    • Temporary — Temporary data initializes at the start of every function call. Valid only in charts that use C as the action language.

    In charts that use C as the action language, define local data when you want your data values to persist across function calls throughout the simulation. Define temporary data when you want to initialize data values at the start of every function call. For example, you can define a counter with Local scope if you want to track the number of times that you call the function. In contrast, you can designate a loop counter to have Temporary scope if you do not need the counter value to persist after the function completes.

    In charts that use MATLAB® as the action language, you do not need to define local or temporary data in graphical functions. Instead, you can use undefined variables to store values that are accessible only during the rest of the function call. To store values that persist across function calls, use local data at the chart level.

    Tip

    You can initialize local and temporary data in your function from the MATLAB workspace. For more information, see Initialize Data from the MATLAB Base Workspace.

Call Graphical Functions in States and Transitions

You can call graphical functions from the actions of any state or transition or from other functions. If you export a graphical function, you can call it from any chart in the model. For more information about exporting functions, see Export Stateflow Functions for Reuse.

To call a graphical function, use the function signature and include an actual argument value for each formal argument in the function signature.

[return_val1,return_val2,...] = function_name(arg1,arg2,...)

If the data types of the actual and formal arguments differ, the function casts the actual argument to the type of the formal argument.

Manage Large Graphical Functions

You can choose to make your graphical function as large as you want. If your function grows too large, you can hide its contents by right-clicking inside the function box and selecting Group & Subchart > Subchart from the context menu.

A subcharted graphical function with its content preview enabled.

To make the graphical function box opaque, right-click the function and clear the Content Preview property from the context menu.

A subcharted graphical function with its content preview disabled.

To dedicate the entire chart window to programming your function, access the flow chart in your subcharted graphical function by double-clicking the function box. For more information, see Encapsulate Modal Logic by Using Subcharts.

Specify Properties of Graphical Functions

The properties listed below specify how a graphical function interacts with the other components in your Stateflow chart. You can modify these properties in the Property Inspector, the Model Explorer, or the Function properties dialog box.

To use the Property Inspector:

  1. In the Modeling tab, under Design Data, select Property Inspector.

  2. In the Stateflow Editor, select the graphical function.

  3. In the Property Inspector, edit the transition properties.

To use the Model Explorer:

  1. In the Modeling tab, under Design Data, select Model Explorer.

  2. In the Model Hierarchy pane, select the graphical function.

  3. In the Dialog pane, edit the graphical function properties.

To use the Function properties dialog box:

  1. In the Stateflow Editor, right-click the graphical function.

  2. Select Properties.

  3. In the properties dialog box, edit the graphical function properties.

You can also modify these properties programmatically by using Stateflow.Function objects. For more information about the Stateflow programmatic interface, see Overview of the Stateflow API.

Name

Function name. Click the function name link to bring your function to the foreground in its native chart.

Function Inline Option

Controls the inlining of your function in generated code:

  • Auto — Determines whether to inline your function based on an internal calculation.

  • Inline — Inlines your function if you do not export it to other charts and it is not part of a recursion. (A recursion exists if your function calls itself directly or indirectly through another function call.)

  • Function — Does not inline your function.

This property is not available in the Property Inspector.

Label

Signature label for your function. The function signature label specifies a name for your function and the formal names for its arguments and return values. This property is not available in the Property Inspector.

Description

Description of the graphical function.

Document Link

Link to online documentation for the graphical function. You can enter a web URL address or a MATLAB command that displays documentation as an HTML file or as text in the MATLAB Command Window. When you click the Document link hyperlink, Stateflow evaluates the link and displays the documentation.

See Also

Objects

Tools

Related Topics