Stateflow | ![]() ![]() |
Creating a Graphical Function
To create a graphical function:
A function can reside anywhere in a diagram, either at the top level or within any state or subchart. The location of a function determines its scope, that is, the set of states and transitions that can invoke the function. In particular, the scope of a function is the scope of its parent state or chart, with the following exceptions.
In this case, the scope of the function is the scope of its parent state machine. See Exporting Graphical Functions for more information.
In this case, the function defined in the parent is not visible anywhere in the child or its children. In other words, a function defined in a state or subchart shadows any functions of the same defined in the ancestors of that state or subchart.
Stateflow converts the state to a graphical function.
The new function appears as an unnamed object in the Stateflow Explorer.
The function prototype specifies a name for the function and formal names for its arguments and return value. A prototype has the syntax
y = f(a1,a2,...an)
where f
is the function's name, a
1, a
2, a
n are formal names for its arguments, and y
is the formal name for its return value. The following example shows a prototype for a graphical function named f1
that takes two arguments and returns a value.
The return values and arguments that you declare in the prototype appear
in the Explorer as data items parented by the function object.
The Scope field in the Explorer indicates the role of the corresponding
argument or return value. Arguments have scope Input
. Return values have
scope Output
. The number that appears in parentheses for the scope of each
argument is the order in which the argument appears in the function's
prototype. When a Stateflow action invokes a function, it passes arguments
to the function in the same order.
In the context of graphical function prototypes, the term scope refers to the role (argument or return value) of the data items specified by the function's prototype. The term scope can also refer to a data item's visibility. In this sense, arguments and return values have local scope. They are visible only in the flow diagram that implements the function.
SeeSetting Data Properties for information on setting data properties. The following restrictions apply to argument and return value properties.
Input
.Output
.See Adding Data to the Data Dictionary for information on how to create data items. A function can access only items that it owns. Thus, any items that you create for use by the function must be created as children of the function. The items that you create can have any of the following scopes.
Local
A local data item persists from invocation to invocation. For example, if the item is equal to 1 when the function returns from one invocation, the item will equal 1 the next time the function is invoked.
Temporary
Stateflow creates and initialize a copy of a temporary item for each invocation of the function.
Constant
A constant data items retains its initial value through all invocations of the function.
All data items (other than arguments and return values) parented by a graphical function can be initialized from the workspace. However, only local items can be saved to the workspace.
At a minimum, the flow diagram must include a default transition
terminated by a junction. The following example shows a minimal flow
diagram for a graphical function that computes the product of its
arguments.
![]() | Working with Graphical Functions | Invoking Graphical Functions | ![]() |