Using Graphical Functions to Extend Actions

What Is a Graphical Function?

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.

Why Use a Graphical Function?

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.

Where to Use a Graphical 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:

Workflow for Defining a Graphical Function

Creating a Graphical Function

Use these steps to create a graphical function in your chart:

  1. Click the graphical function icon in the Stateflow Editor toolbar:

  2. Move your pointer to the location for the new graphical function in your chart and click to insert the function box.

  3. Enter the function signature.

    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.

  4. 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).

Programming a Graphical Function

To program a graphical function, follow these steps:

  1. Click the default transition icon in the Stateflow Editor toolbar:

  2. Move your pointer inside the function box in your chart and click to insert the default transition and its terminating junction.

  3. 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.

Defining Graphical Function Data

You must define the data in your graphical function:

  1. 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.

  2. 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.

  3. For each function argument and return value, right-click the data row in the Model Explorer and select Properties from the context menu.

  4. In the Data properties dialog for each argument and return value, specify the data properties.

    These rules apply:

  5. 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:

Managing Large Graphical Functions

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 .

Calling Graphical Functions in Stateflow® Action Language

Description

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

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.

Example

In this example, a state entry action calls a graphical function that returns three products.

Exporting Chart-Level Graphical Functions

Why Export Graphical Functions?

When you export chart-level graphical functions, you extend the scope of your functions to all other charts in your model.

How to Export Chart-Level Graphical Functions

Perform these steps to export graphical functions to your main model:

  1. Open the chart where your graphical function resides.

  2. In the Stateflow Editor, select File > Chart Properties.

  3. In the Chart Properties dialog, select Export Chart Level Graphical Functions (Make Global).

  4. If your function resides in a library chart, link that chart to your main model.

Example of Exporting Chart-Level Graphical Functions

This example describes how to export graphical functions in library charts to your main model.

  1. Create these objects:

  2. Create these graphical functions:

  3. Add a default transition to modChart.

  4. For each chart, select File > Chart Properties in the Stateflow Editor.

  5. In the properties dialog for each chart, select the Export Chart Level Graphical Functions (Make Global) option.

  6. 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.

After you export your graphical functions from the library charts, the sequence of actions in main_model is:

  1. modChart calls the graphical function lib1_func, which takes two arguments, x and y.

  2. lib1_func calls the graphical function lib2_func, which passes the same two arguments.

  3. lib2_func calls the graphical function mod_func, which adds x and y.

  4. x takes the result of the addition.

Specifying Graphical Function Properties

You can set general properties for your graphical function through its properties dialog:

  1. Right-click your graphical function box.

  2. Select Properties from the context menu.

    The properties dialog for your graphical function appears.

The fields in the properties dialog are:

Field

Description

Name

Click this read-only function name to bring your function to the foreground in its native chart.

Breakpoints

Select Function Call to set a breakpoint that pauses simulation when your graphical function executes.

Function Inline Option

Select one of these options to control the inlining of your function in generated code:

  • Auto
    Decides whether or not to inline your function based on an internal calculation.

  • Inline
    Inlines your function as long as 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.

Label

Specify the signature label for your function in this field. See Creating a Graphical Function for more information.

Description

Enter a textual description or comment.

Document Link

Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit/spec/data/speed.txt.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS