| Contents | Index |
| On this page… |
|---|
Methods for adding a Stateflow truth table to a Simulink model are:
| Procedure | Action Languages Supported | How To Do It |
|---|---|---|
| Add a Truth Table block directly to the model. | MATLAB only | See the Truth Table block reference page. |
| Add a Stateflow block that calls a truth table function. | Stateflow Classic and 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 include:
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 model with a Stateflow block appears.

Click and drag the Stateflow block to the center of the model window.
This step makes room for the blocks you add in the steps that follow.
In the model 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 blocks of the Sources library.
From the right pane of the Simulink Library Browser, click and drag the Constant block to the left of the Chart block in the 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 Block Parameters dialog box that appears, change Constant value to 0.
Click OK to close the dialog box.
In the model, double-click the bottom Constant block.
In the Block Parameters dialog box that appears, change Constant value to 0.
Click OK to close the dialog box.
Your model should now look something like this:

Open the Configuration Parameters dialog box.
Type to Fixed-step
Solver to discrete (no continuous states)
Fixed-step size to 1
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 chart for the block and specify a truth table for it:
In your model, double-click the Chart block to open an empty chart.
Click the Truth Table drawing tool:
![]()
Move your pointer into the empty chart and notice that it appears in the shape of a box.
Click to place a new truth table.
A shaded box appears with the title truthtable.
t = ttable(x,y,z)
and click outside the truth table box.

The signature label of the truth table function follows 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.
After you add a truth table function to a 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. |
Click OK to close the dialog box.
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 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 chart in the model.
To call the ttable function from the default transition of its own chart, follow these steps:
From the toolbar, select the Default Transition tool:
![]()
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 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 chart should look something like this:

The label on the default transition 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 executes and a call to the truth table ttable occurs.
The call to the truth table in Stateflow action language must match the truth table signature. 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 chart.
Tip If the formal arguments of a function signature are scalars, verify that inputs and outputs of function calls follow the rules of scalar expansion. For more information, see How Scalar Expansion Works for Functions. |
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 chart as follows:
Double-click the truth table function to open the Truth Table Editor.
In the Truth Table Editor, select Add > Edit Data/Ports.
The Model Explorer appears.

In the Model Hierarchy pane, the node for the function ttable appears highlighted, and the Contents pane displays the output (t) and inputs (x, y, z) for ttable. By default, these data are scalars of type double. If you want to redefine these data with a different 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 chart that contains the truth table ttable.
In the Model Hierarchy pane, select Chart.
Notice that Chart contains no input or output data in the Contents pane. You must add the return and argument data for 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.
In the Contents pane, double-click the entry data in the Name column.
A small text field opens with the name data highlighted.
Under Scope, click the entry Local .
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 passes to the chart through an input port on the Stateflow block.
You should now see the new data input a in the Contents pane.
Repeat steps 4 through 8 to add the data b and c with the scope Input, and data d with the scope Output.
The scope Output means that the chart provides this data, which passes to the Simulink model through an output port on the Stateflow block.
You should now see the input and output 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 model.

Complete connections to the Simulink blocks:

![]() | Workflow for Using Truth Tables | Programming a Truth Table | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |