Program a Truth Table
After you create a truth table, you can program it to execute according to your specifications. To program a truth table you add conditions, decisions, and actions. For more information about creating a truth table, see Use Truth Tables to Model Combinatorial Logic.
Truth tables are not supported in standalone Stateflow® charts in MATLAB®. For more information, see Use Truth Tables to Model Combinatorial Logic.
Open a Truth Table for Editing
After you create and label a truth table in a chart or Simulink® model, you specify its logical behavior. These specifications apply to both the truth table block in a Simulink model and the truth table function in a Stateflow chart. In this example, you specify the behavior of a truth table function.
To open the truth table, double-click the truth table function,
ttable
, that you created in Use Truth Tables to Model Combinatorial Logic.
By default, a truth table contains a Condition Table and an Action Table, each with one row. The Condition Table contains a single decision column, D1, and a single action row.
Select an Action Language
If the truth table is inside a Stateflow chart that uses C as the action language, you can specify the action language for your Stateflow truth table:
Open the Property Inspector. In the Modeling tab, under Design Data, select Property Inspector.
Under the Properties section, select C or MATLAB as the Action Language.
Stateflow charts that use MATLAB as the action language only support truth tables that use MATLAB as the action language.
Enter Truth Table Conditions
Conditions are the starting point for specifying logical behavior in a truth table. Open
your new truth table, ttable
, for editing. 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 enter an
optional description in the Description column. To enter conditions for
the truth table ttable
:
Click the row on the Condition Table that you want to append.
Click the Append Row button on the side panel twice.
The truth table 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.
Enter this text:
x is equal to 1
Click the next cell on the right, in the Condition column..
In the first cell of the Condition column, enter:
XEQ1:
This text is an optional label that 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 this text:
x == 1
This text is the actual condition. Each condition that you enter must evaluate to zero (false) or nonzero (true). You can use optional brackets in the condition (for example,
[x == 1]
).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.
Enter Truth Table Decisions
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 Enter Truth Table Conditions, you entered
conditions for the truth table ttable
. Continue by entering values in the
decision columns:
Click the decision column of the Condition Table that you want to append.
Click the Append Column button on the side panel three times.
Click the top cell in decision column D1.
Press the space bar until a value of
T
appears.Pressing the space bar toggles through the possible values of
F
,T
, and-
. You can also enter these characters directly. Pressing 1 sets the value toT
, while pressing 0 sets the value toF
. Pressingx
sets the value to-
.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 Default Decision Column
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. Create a default decision by entering - in every
cell of the farthest right decision column. This entry represents any outcome for the
condition, T
or F
. The default decision column must
be the last column on the right in the Condition Table.
Enter Truth Table Actions
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 that is specified in the Actions row for that decision column executes. Then the truth table exits.
In Enter Truth Table Decisions, you entered decisions in the truth table. 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.
Set Up the Action Table
Click the row Action Table that you want to append.
Click the Append Row button on the side panel three times.
Program the actions for the truth table.
Program Actions of a Truth Table
For truth tables that use MATLAB as the 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 flowsfor
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.
Enter this description:
Maintain a counter and a circular vector of values of length 6. Every time this action is called, output r takes the next value of the vector.
Press the right arrow key to select the next cell on the right, in the Action column.
Enter the following text:
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 this code:
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 r takes the next value in values vector r = 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
r
equal to the next value of the vectorvalues
. 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 thevalues
vector appears:
Now you are ready to assign actions to decision.
Assign 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.
Rules for Assigning Actions to Decisions
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
or1;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.
How to Assign Actions to Decisions
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.
Add 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, right-click row 1 of the Action Table and select Insert Row.
A blank row appears at the top of the Action Table.
In the Modeling tab, select Append Row.
A blank row appears at the bottom of the Action Table.
Click and drag the bottom border of the truth table to show all six rows of the Action Table:
Add the initial action in row 1 as follows:
Description Action Initial action:
Display message
INIT:
coder.extrinsic("disp");
disp("truth table ttable entered");
Add the final action in row 6 as follows:
Description Action 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.
Tip
You can automatically adjust the height of the truth table rows by pressing Ctrl+Shift+F.