| Stateflow® | ![]() |
| On this page… |
|---|
There are several ways to add a Stateflow truth table to a Simulink model:
| Procedure | Action Languages Supported | How To Do It |
|---|---|---|
| Add a Truth Table block directly to the model | Embedded MATLAB only | See Truth Table. |
| Add a Stateflow block that calls a truth table function | Stateflow Classic and Embedded MATLAB | See Adding a Stateflow Block that Calls a Truth Table Function. |
This section describes how to add a Stateflow block to your Simulink model, and then create a chart that calls a truth table function. These topics are covered:
Specifying Properties of Truth Table Functions in Stateflow Charts
Creating Truth Table Data in Stateflow Charts and Simulink Models
Once you build a model in this section, finish it by programming the truth table with its behavior in Programming a Truth Table.
To execute a truth table, you first need a Simulink model that calls a Stateflow block. Later, you will create a Stateflow chart for the Stateflow block that calls a truth table function. In this section, you create a Simulink model that calls a Stateflow block with the following procedure:
At the MATLAB prompt, enter the following command:
sfnew
An untitled Simulink model with a Stateflow block appears.

Click and drag the Stateflow block to the center of the Simulink window.

This action makes room for the blocks you add in the steps that follow.
In the Simulink window, select View > Library Browser.
The Simulink Library Browser window opens with the Simulink node expanded.
Under the Simulink node, select the Sources library.
The right pane of the Simulink Library Browser window displays the block members of the Sources library.
From the right pane of the Simulink Library Browser window, click and drag the Constant block to the left of the Stateflow block in the Simulink model.
Add two more Constant blocks to the left of the Chart block, and add a Display block (from the Sinks library) to the right of the Chart block.
In the Simulink model window, double-click the middle Constant block.
In the resulting Block Parameters dialog box, change the Constant value field to 0.
Click OK to close the dialog box.
In the Simulink model window, double-click the bottom Constant block.
In the resulting Block Parameters dialog box, change the Constant value field to 0.
Click OK to close the dialog box.
Your model should now have the following appearance.

In the Simulink model window, from the Simulation menu, select Configuration Parameters.
The Configuration Parameters dialog box opens.
Solver Options Type field to Variable-step
Stop Time to inf
Click OK to accept these values and close the Configuration Parameters dialog box.
You created a Simulink model in Creating a Simulink Model that contains a Stateflow block. Now you need to open the Stateflow chart for the block and specify a truth table for it in these steps:
In the Simulink model, double-click the Stateflow block named Chart.
An empty Stateflow Editor appears.

In the Stateflow Editor, click the Truth Table drawing tool:
![]()
Move your pointer into the empty chart area and notice that it appears in the shape of a box.
Click to place a new truth table as shown.

A shaded box appears with the title truthtable and a flashing text cursor in the middle of the box.
t = ttable(x,y,z)
and click outside the truth table box.

You must label a truth table function with its signature. Use the following syntax:
[return_val1, return_val2,...] = function_name(arg1, arg2,...)
You can specify multiple return values and multiple input arguments, as shown in the syntax. Each return value and input argument can be a scalar, vector, or matrix of values.
After you add a truth table function to a Stateflow chart, you can specify its properties by following these steps:
Select Properties from the context menu.
The Truth Table properties dialog box for the truth table function appears.

The fields in the Truth Table properties dialog box are as follows:
Field | Description |
|---|---|
Function name; read-only; click this hypertext link to bring the truth table function to the foreground in its native Stateflow chart. | |
Select Function Call to set a breakpoint to pause execution during simulation when the truth table function is called. | |
This option controls the inlining of the truth table function in generated code through the following selections:
| |
You can specify the signature label for the function through this field. See Creating a Stateflow Truth Table for more information. | |
Textual description/comment. | |
Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit/spec/data/speed.txt. |
In Creating a Stateflow Truth Table, you created the truth table function ttable with the signature
t = ttable(x,y,z)
Now you need to specify a call to the truth table function in the Stateflow chart. Later, when the chart executes during simulation, it calls the truth table.
You can call truth table functions from the actions of any state or transition. You can also call truth tables from other functions, including graphical functions and other truth tables. Also, if you export a truth table, you can call it from any Stateflow chart in the model.
Use these steps to call the ttable function from the default transition of its own Stateflow chart.
Select the Default Transition button
from the drawing toolbar.
Move your pointer to a location left of the truth table function and notice that it appears in the shape of a downward-pointing arrow.
Click to place a default transition into a terminating junction.

Click the question mark character (?) that appears on the highlighted default transition.
A blinking cursor in a text field appears for entering the label of the default transition.
{d = ttable(a,b,c);}
and click outside the transition label to finish editing it.
You might want to adjust the label's position by clicking and dragging it to a new location. The finished Stateflow chart should have the following appearance:

The label on the default transition that you entered provides a condition action that calls the truth table with arguments and a return value. When the Simulink model triggers the Stateflow block during simulation, the default transition is taken and a call to the truth table ttable is made.
The call to the truth table in Stateflow action language must match the truth table signature. This means that the type of the return value d must match the type of the signature return value t, and the type of the arguments a, b, and c must match the type of the signature arguments x, y, and z. You ensure this with a later step in this section when you create the data that you use in the Stateflow chart.
When you create a truth table with its own signature, you specify data for it in the form of a return value (t) and argument values (x, y, z). When you specify a call to a truth table, as you did in Calling a Truth Table in a Stateflow Action, you specify data that you pass to the return and argument values of the truth table (d, a, b, and c). Now you must define this data for the Stateflow chart in these steps:
Double-click the truth table to open the Truth Table Editor.
In the Truth Table Editor, select the Edit
Data/Ports button
.
The Model Explorer appears.

In the Model Hierarchy pane, the node for the function ttable is highlighted, and the Contents pane displays the output (t) and inputs (x, y, z) for ttable. By default, these data are defined as scalars of type double. If you want to redefine these data with a different array size and type, you do it in the Model Explorer. However, no changes are necessary for this example.
Notice also in the Model Hierarchy pane that the node above the function ttable is Chart, the name of the Stateflow chart that contains the truth table ttable.
In the Model Hierarchy pane, select Chart.
Notice that Chart contains no data in the Contents pane. You need to add the return and argument data used in calling ttable.
A scalar data is added to the chart in the Contents pane of the Model Explorer with the default name data. This data matches argument x in type and size.
Tip To verify that the properties match, right-click data in the Contents pane and select Properties. The property sheet shows that the type is double and the size is scalar (the default when there is no entry in the Size field). |
In the Contents pane, double-click the entry data in the Name column.
A small text field opens with the name data highlighted.
Click the entry Local under the Scope column.
A drop-down menu of selectable scopes appears with Local selected.
The scope Input means that the Simulink model provides the value for this data, which it passes to the Stateflow chart through an input port on the Stateflow block.
You should now see the new data input a in the Contents pane.
Repeat steps 3 through 7 to add the data b and c with the scope Input, and data d with a scope of Output.
The scope Output means that the Stateflow chart provides this data and passes it to the Simulink model through an output port on the Stateflow block.
You should now see the following data in the Model Explorer.

The data a, b, c, and d match their counterparts x, y, z, and t in the truth table signature in size (scalar) and type (double), but have sources outside the Stateflow block. Notice that input ports for a, b, and c, and an output port for d appear on the Stateflow block in the Simulink model.

Complete connections to the Simulink blocks as shown.

![]() | Workflow for Using Truth Tables | Programming a Truth Table | ![]() |
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |