| Contents | Index |
| On this page… |
|---|
Opening a Truth Table for Editing Entering Truth Table Conditions Entering Truth Table Decisions |
After you create and label a truth table in a chart, you specify its logical behavior. Double-click the truth table function to open the Truth Table Editor.

By default, a truth table contains a Condition Table and an Action Table, each with one row. The Condition Table also contains a single decision column, D1, and a single action row.
Select the language you want to use for programming conditions and actions in your truth table:
In the Truth Table Editor, select Settings > Language.
Choose a language from the drop-down menu.
Conditions are the starting point for specifying logical behavior in a truth table. You open the truth table ttable for editing in Opening a Truth Table for Editing. In this topic, you start programming the behavior of ttable by specifying conditions.
You enter conditions in the Condition column of the Condition Table. For each condition that you enter, you can also enter an optional description in the Description column. Use the following procedure to enter conditions for the truth table ttable:
Select Edit > Append Row twice.
The editor appends two rows to the bottom of the Condition Table.
Click and drag the bar that separates the Condition Table and the Action Table panes down to enlarge the Condition Table pane.
In the Condition Table, click the top cell of the Description column.
A flashing text cursor appears in the cell, which appears highlighted.
x is equal to 1
Condition descriptions are optional, but appear as comments in the generated code for the truth table.
Press the Tab key to select the next cell on the right in the Condition column.
In the first cell of the Condition column, enter the following text:
XEQ1:
This text is an optional label you can include with the condition. Each label must begin with an alphabetic character ([a-z][A-Z]) followed by any number of alphanumeric characters ([a-z][A-Z][0-9]) or an underscore (_).
Press Enter and enter the following text:
x == 1
This text is the actual condition. Each condition you enter must evaluate to zero (false) or nonzero (true). You can use optional brackets in the condition (for example, [x == 1]) as you do in Stateflow action language.
In truth table conditions, you can use data that passes to the truth table function through its arguments. The preceding condition tests whether the argument x is equal to 1. You can also use data defined for parent objects of the truth table, including the chart.
Repeat the preceding steps to enter the other two conditions.

Each decision column (D1, D2, and so on) binds a group of condition outcomes together with an AND relationship into a decision. The possible values for condition outcomes in a decision are T (true), F (false), and - (true or false). In Entering Truth Table Conditions you entered conditions for the truth table ttable. Continue by entering values in the decision columns:
Select Edit > Append Column three times to add three columns to the right end of the Condition Table.
Click the top cell in decision column D1.
A flashing text cursor appears in the cell, which appears highlighted.
Press the space bar until a value of T appears.
Pressing the space bar toggles through the possible values of F, -, and T. You can also enter these characters directly. The editor rejects all other entries.
Press the down arrow key to advance to the next cell down in the D1 column.
In the decision columns, you can use the arrow keys to advance to another cell in any direction. You can also use Tab and Shift+Tab to advance left or right in these cells.
Enter the remaining values for the decision columns:

During execution of the truth table, decision testing occurs in left-to-right order. The order of testing for individual condition outcomes within a decision is undefined. Truth tables evaluate the conditions for each decision in top-down order (first condition 1, then condition 2, and so on). Because this implementation is subject to change in the future, do not rely on a specific evaluation order.
The last decision column in ttable, D4, is the default decision for this truth table. The default decision covers any decisions not tested for in preceding decision columns to the left. You enter a default decision as the last decision column on the right with an entry of - for all conditions in the decision. This entry represents any outcome for the condition, T or F.
In the preceding example, the default decision column, D4, specifies these decisions:
Condition | Decision 4 | Decision 5 | Decision 6 | Decision 7 | Decision 8 |
|---|---|---|---|---|---|
x == 1 | F | T | F | T | T |
y == 1 | F | F | T | T | T |
z == 1 | F | T | T | F | T |
During execution of the truth table, decision testing occurs in left-to-right order. When a decision match occurs, the action in the Action Table specified in the Actions row for that decision column executes. Then the truth table exits.
In Entering Truth Table Decisions, you entered decisions in the Truth Table Editor. The next step is to enter actions you want to occur for each decision in the Action Table. Later, you assign these actions to their decisions in the Actions row of the Condition Table.
This section describes how to program truth table actions with these topics:
Setting Up the Action Table — Shows you how to set up the Action Table in truth table ttable.
Programming Actions in Stateflow Classic Action Language — Provides sample code in Stateflow action language to program actions in ttable. Follow this section if you selected Stateflow Classic as the language for this truth table.
Programming Actions in MATLAB Action Language — Provides sample MATLAB code to program actions in ttable. Follow this section if you selected MATLAB as the language for this truth table.
Select Edit > Append Row three times to add three rows to the bottom of the Action Table:

Program the actions using the language you selected for the truth table.
| If you selected... | Use this procedure... |
|---|---|
| Stateflow Classic | Programming Actions in Stateflow Classic Action Language |
| MATLAB | Programming Actions in MATLAB Action Language |
Follow this procedure to program your actions in Stateflow action language:
Click the top cell in the Description column of the Action Table.
A flashing text cursor appears in the cell, which appears highlighted.
Enter the following description:
set t to 1
Action descriptions are optional, but appear as comments in the generated code for the truth table.
Press Tab to select the next cell on the right, in the Action column.
A1:
You begin an action with an optional label followed by a colon (:). Later, you enter these labels in the Actions row of the Condition Table to specify an action for each decision column. Like condition labels, action labels must begin with an alphabetic character ([a-z][A-Z]) followed by any number of alphanumeric characters ([a-z][A-Z][0-9]) or an underscore (_).
Press Enter and enter the following text:
t=1;
In truth table actions, you can use data that passes to the truth table function through its arguments and return value. The preceding action, t=1, sets the value of the return value t. You can also specify actions with data defined for a parent object of the truth table, including the chart. Truth table actions can also broadcast or send events that are defined for the truth table, or for a parent, such as the chart itself.
Enter the remaining actions in the Action Table, as shown:

Now you are ready to assign actions to decisions, as described in Assigning Truth Table Actions to Decisions.
If you selected MATLAB action language, you can write MATLAB code to program your actions. Using this code, you can add control flow logic and call MATLAB functions directly. In the following procedure, you program an action in the truth table ttable, using the following features of MATLAB syntax:
Persistent variables
if ... else ... end control flows
for loop
Follow these steps:
Click the top cell in the Description column of the Action Table.
A flashing text cursor appears in the cell, which appears highlighted.
Maintain a counter and a circular vector of values of length 6. Every time this action is called, output t takes the next value of the vector.
Action descriptions are optional, but appear as comments in the generated code for the truth table.
Press Tab to select the next cell on the right, in the Action column.
A1:
You begin an action with an optional label followed by a colon (:). Later, you enter these labels in the Actions row of the Condition Table to specify an action for each decision column. Like condition labels, action labels must begin with an alphabetic character ([a-z][A-Z]) followed by any number of alphanumeric characters ([a-z][A-Z][0-9]) or an underscore (_).
Press Enter and enter the following text:
persistent values counter;
cycle = 6;
coder.extrinsic('plot');
if isempty(counter)
% Initialize counter to be zero
counter = 0;
else
% Otherwise, increment counter
counter = counter + 1;
end
if isempty(values)
% Values is a vector of 1 to cycle
values = zeros(1, cycle);
for i = 1:cycle
values(i) = i;
end
% For debugging purposes, call the MATLAB
% function "plot" to show values
plot(values);
end
% Output t takes the next value in values vector
t = values( mod(counter, cycle) + 1);
In truth table actions, you can use data that passes to the truth table function through its arguments and return value. The preceding action sets the return value t equal to the next value of the vector values. You can also specify actions with data defined for a parent object of the truth table, including the chart. Truth table actions can also broadcast or send events that are defined for the truth table, or for a parent, such as the chart itself.
Enter the remaining actions in the Action Table, as shown:

If action A1 executes during simulation, a plot of the values vector appears:

Now you are ready to assign actions to decisions, as described in Assigning Truth Table Actions to Decisions.
You must assign at least one action from the Action Table to each decision in the Condition Table. The truth table uses this association to determine what action to execute when a decision tests as true.
The following rules apply when you assign actions to decisions in a truth table:
You specify actions for decisions by entering a row number or a label in the Actions row cell of a decision column.
If you use a label specifier, the label must appear with the action in the Action Table.
You must specify at least one action for each decision.
Actions for decisions are not optional. Each decision must have at least one action specifier that points to an action in the Action Table. If you want to specify no action for a decision, specify a row that contains no action statements.
You can specify multiple actions for a decision with multiple specifiers separated by a comma, semicolon, or space.
For example, for the decision column D1, you can specify A1,A2,A3 or 1;2;3 to execute the first three actions when decision D1 is true.
You can mix row number and label action specifiers interchangeably in any order.
The following example uses both row and label action specifiers.

You can specify the same action for more than one decision, as shown:

Row number action specifiers in the Actions row of the Condition Table automatically adjust to changes in the row order of the Action Table.
This section describes how to assign actions to decisions in the truth table ttable. In this example, the Actions row cell for each decision column contains a label specified for each action in the Action Table. Follow these steps:
Click the bottom cell in decision column D1, the first cell of the Actions row of the Condition Table.
Enter the action specifier A1 for decision column D1.
When D1 is true, action A1 in the Action Table executes.
Enter the action specifiers for the remaining decision columns:

Now you are ready to perform the final step in programming a truth table, Adding Initial and Final Actions.
In addition to actions for decisions, you can add initial and final actions to the truth table function. Initial actions specify an action that executes before any decision testing occurs. Final actions specify an action that executes as the last action before the truth table exits. To specify initial and final actions for a truth table, use the action labels INIT and FINAL in the Action Table.
Use this procedure to add initial and final actions that display diagnostic messages in the MATLAB Command Window before and after execution of the truth table ttable:
In the Truth Table Editor, right-click row 1 of the Action Table and select Insert Row.
A blank row appears at the top of the Action Table.
A blank row appears at the bottom of the Action Table.
Click and drag the bottom border of the Truth Table Editor to show all six rows of the Action Table:

Add the initial action in row 1 as follows:
| Truth Table Type | Description | Action |
|---|---|---|
| Stateflow Classic | Initial action: Display message | INIT: ml.disp('truth table ttable entered'); |
| MATLAB | Initial action: Display message | INIT: coder.extrinsic('disp'); disp('truth table ttable entered'); |
Add the final action in row 6 as follows:
| Truth Table Type | Description | Action |
|---|---|---|
| Stateflow Classic | Final action: Display message | FINAL: ml.disp('truth table ttable exited'); |
| MATLAB | Final action: Display message | FINAL: coder.extrinsic('disp'); disp('truth table ttable exited'); |
Although the initial and final actions for the preceding truth table example appear in the first and last rows of the Action Table, you can enter these actions in any row. You can also assign initial and final actions to decisions by using the action specifier INIT or FINAL in the Actions row of the Condition Table.
![]() | Building a Model with a Stateflow Truth Table | Debugging 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 |