Truth tables implement combinatorial logic design in a concise, tabular format. Truth tables are supported only in Stateflow® blocks in a Simulink® model. For more information, see Reuse Combinatorial Logic by Defining Truth Tables.
Stateflow software realizes the logical behavior specified in a truth table by generating content as follows:
|Type of Truth Table||Generated Content|
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.
Click the View Generated Content button:
This section describes how Stateflow software translates the logic of a C truth table into a graphical function.
In the following example, a C 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 chart looks like this:
The top half of the flow chart 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 chart looks like this:
In the bottom half of the flow chart, 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
action and termination of the flow chart.
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 chart 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:
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 Program Actions Using MATLAB Expressions:
Main truth table function
function r = 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(); end
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 r takes the next value in values vector r = values( mod(counter, cycle) + 1);
function A2() % Action #2, "A2" % set r to 2 r=2; %================================== function A3() % Action #3, "A3" % set r to 3 r=3; %================================== function A4() % Action #4, "A4" % set r to 4 r=4;