Stateflow User's Guide Previous page   Next Page

Programming Functions

In Creating a Function, you learn how to create a function with a return value and arguments in a Stateflow diagram. In the context of programming a function, arguments and return values have local scope. They are visible only in the environment that programs the function. This topic introduces you to the environment for each function in which you program its behavior with the function subtopics that follow.

Graphical Functions

A Stateflow graphical function is a program written with Stateflow flow graphs using connective junctions and transitions. You create a graphical function, fill it with a flow diagram, and call it repeatedly in the actions of states and transitions.

Because a function must execute completely when it is called, states are not allowed in graphical functions. When a state is entered, execution stops until an event occurs.

At a minimum, the flow diagram must include a default transition with a terminating junction. The following example shows a minimal flow diagram for a graphical function that returns the product of its arguments.

You can make a graphical function as complicated and as long as you want. However, because complicated graphical functions can become very large, it may be difficult to fit it into the Stateflow diagram. To make the function smaller, hide the function's contents by selecting Subcharted from the Make Contents menu of the function's shortcut menu. This makes the graphical function opaque as shown.

To access the programming of a subcharted graphical function, double-click it. This dedicates the entire Stateflow diagram window to programming the function, as shown.

To access the original Stateflow diagram, select the Back button .

Truth Table Functions

You program a truth table in the truth table editor. Access this editor for a truth table by double-clicking it in the Stateflow diagram, and the truth table editor appears.

Truth table functions in Stateflow are very suitable for implementing functions with logical behavior. You program truth tables with logical behavior in the form of action language conditions, decisions, and actions. For example, the following truth table

implements the following logic expressed in pseudo-code:

Description
Pseudo-Code
Decision 1
Decision 1 Action
  • if ((x == 1) & !(y == 1) & !(z == 1))
        t = 1;
    
Decision 2
Decision 2 Action
  • elseif (!(x == 1) & (y == 1) !(z == 1))
        t = 2;
    
Decision 3
Decision 3 Action
  • elseif (!(x == 1) & !(y == 1) (z == 1))
        t = 3;
    
Default Decision
Default Decision Action
  • else
        t = 4;
    endif
    

When you try to simulate your truth tables, Stateflow detects whether they are underspecified or overspecified. Underspecified truth tables contain fewer than all possible decisions for the conditions you specify. Overspecified truth tables contain redundant decisions that prevent other decisions from being evaluated. Stateflow detects both underspecified and overspecified truth tables and tells you the decisions you need to specify or remove in the truth table.

See Truth Table Functions for a thorough understanding of truth table functions in Stateflow.

Embedded MATLAB Functions

You program an embedded MATLAB function in the Embedded MATLAB Editor. Access this editor for an embedded MATLAB function by double-clicking it in the Stateflow diagram. The following is an example of a newly created function in Stateflow that you open to program:

You program embedded MATLAB functions just like you would a MATLAB function. Embedded MATLAB functions in Stateflow use a rich subset of MATLAB language and functions to generate code for applications that can reside in target applications with operating systems and platforms that have strict memory and data type requirements.

The following embedded MATLAB function calculates a mean and a standard deviation for a set of value input to the function as a vector argument:

For a detailed example and information on programming embedded MATLAB functions in Stateflow, see Using Embedded MATLAB Functions.


Previous page  Creating a Function Defining Function Data Next page

© 1994-2005 The MathWorks, Inc.