| Contents | Index |
| On this page… |
|---|
How Stateflow Software Generates Graphical Functions for Truth Tables How Stateflow Software Generates MATLAB Code for Truth Tables |
Stateflow software realizes the logical behavior specified in a truth table by generating content as follows:
| Type of Truth Table | Generated Content |
|---|---|
| Stateflow Classic | Graphical function |
| MATLAB | MATLAB code |
You generate content for a truth table when you simulate your model. Content regenerates whenever a truth table changes. To view the generated content of a truth table, follow these steps:
Simulate the model that contains the truth table.
Double-click the truth table to open its editor.
Click the View Generated Content button:
![]()
This section describes how Stateflow software translates the logic of a Stateflow Classic truth table into a graphical function.
In the following example, a Stateflow Classic truth table has three conditions, four decisions and actions, and initial and final actions.

Stateflow software generates a graphical function for the preceding truth table. The top half of the flow graph looks like this:

The top half of the flow graph executes as follows:
Performs initial actions
Evaluates the conditions and stores the results in temporary data variables
The temporary data for storing conditions is based on the labels that you enter for the conditions. If you do not specify the labels, temporary data variables appear.
The bottom half of the flow graph looks like this:

In the bottom half of the flow graph, the stored values for conditions determine which decision is true and what action to perform. Each decision appears as a fork from a connective junction with one of two possible paths:
A transition segment with a decision followed by a segment with the consequent action
The action appears as a condition action that leads to the FINAL action and termination of the flow graph.
A transition segment that flows to the next fork for an evaluation of the next decision
This transition segment has no condition or action.
This implementation continues from the first decision through the remaining decisions in left-to-right column order. When a decision match occurs, the action for that decision executes as a condition action of its transition segment. After the action executes, the flow graph performs the final action for the truth table and terminates. Therefore, only one action results from a call to a truth table graphical function. This behavior also means that no data dependencies are possible between different decisions.
Stateflow software generates the content of MATLAB truth tables as MATLAB code that represents each action as a nested function inside the main truth table function.
Nested functions offer these advantages over subfunctions:
Nested functions are independent of one another. Variables are local to each function and not subject to naming conflicts.
Nested functions can access all data from the main truth table function.
The generated content appears in the function editor, which provides tools for simulation and debugging.
Here is the generated content for the MATLAB truth table described in Programming Actions in MATLAB Action Language:
Main truth table function
function t = ttable(x,y,z)
% Initialize condition vars to logical scalar
XEQ1 = false;
YEQ1 = false;
ZEQ1 = false;
% Condition #1, "XEQ1"
% x is equal to 1
XEQ1 = logical(x == 1);
% Condition #2, "YEQ1"
% y is equal to 1
YEQ1 = logical(y == 1);
% Condition #3, "ZEQ1"
% z is equal to 1
ZEQ1 = logical(z == 1);
if (XEQ1 && ~YEQ1 && ~ZEQ1) % D1
A1();
elseif (~XEQ1 && YEQ1 && ~ZEQ1) % D2
A2();
elseif (~XEQ1 && ~YEQ1 && ZEQ1) % D3
A3();
else % Default
A4();
endAction A1
function A1()
% Action #1, "A1"
% Maintain a counter and a circular vector of length 6.
% Every time this action is called,
% output t takes the next value of the vector.
persistent values counter;
cycle = 6;
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);Actions A2, A3, and A4
function A2() % Action #2, "A2" % set t to 2 t=2; %================================== function A3() % Action #3, "A3" % set t to 3 t=3; %================================== function A4() % Action #4, "A4" % set t to 4 t=4;
![]() | Correcting Overspecified and Underspecified Truth Tables | Truth Table Editor Operations | ![]() |

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 |