Truth tables implement combinatorial logic design in a concise, tabular format. Typical applications for truth tables include decision making for:
Fault detection and management
Mode switching
Truth tables are supported only in Simulink®.
You can add a Truth Table block directly to your Simulink model, or you can define a truth table function in a Stateflow® chart, state, or subchart. Truth Table blocks in a Simulink model execute as a Simulink block, while truth table functions in a Stateflow chart execute only when you call the truth table function. The location of the function determines the set of states and transitions that can call the function.
If you want to call the function anywhere in a chart, put your truth table function at the chart level.
If you want to call the function from within one state or subchart and its substates, put your truth table function in that state or subchart. That function overrides any other functions of the same name in the parents and ancestors of that state or subchart.
If you want to call the function from any chart in your model, put your truth table at the chart level and enable exporting of chart-level functions. For more information, see Export Stateflow Functions for Reuse.
Whether your truth table exists as a block within a Simulink model or as a function within a Stateflow chart, you program them the same way.
This truth table function has the name ttable
. It takes three
arguments (x
, y
, and z
) and returns
one output value (r
).
The function consists of this arrangement of conditions, decisions, and actions.
Condition | Decision 1 | Decision 2 | Decision 3 | Decision 4 |
---|---|---|---|---|
x == 1 | T | F | F | - |
y == 1 | F | T | F | - |
z == 1 | F | F | T | - |
Action | r = 1 | r = 2 | r = 3 | r = 4 |
Each of the conditions entered in the Condition column must evaluate to true (nonzero value) or false (zero value). Outcomes for each condition are specified as T (true), F (false), or - (true or false). Each of the decision columns combines an outcome for each condition with a logical AND into a compound condition, which is referred to as a decision.
You evaluate a truth table one decision at a time, starting with Decision 1. The Decision 4 covers all possible remaining decisions. If one of the decisions is true, the table perform the associated action, and then the truth table execution is complete.
For example, if conditions x == 1
and y == 1
are
false and condition z == 1
is true, then Decision 3
is true and the variable r
is set equal to 3. The remaining decisions are
not tested and evaluation of the truth table is finished. If the first three decisions are
false, then the default decision is automatically true and its action
(r=4
) is executed. This table lists pseudocode corresponding to the
evaluation of this truth table example.
Pseudocode | Description |
---|---|
if ((x == 1) & !(y == 1) & !(z == 1)) r = 1; | If Decision 1 is true, then set
|
elseif (!(x == 1) & (y == 1) & !(z == 1)) r = 2; | If Decision 2 is true, then set
|
elseif (!(x == 1) & !(y == 1) & (z == 1)) r = 3; | If Decision 3 is true, then set
|
else r = 4; endif | If all other decisions are false, then default decision is true. Set
|
To define a truth table function:
Declare the truth table function arguments and return values.
Program the truth table function. See Program a Truth Table.
Use the Property Inspector and Symbols pane to specify the data properties for each argument and return value.
Use the Symbols pane to create any additional data items required by your function. Your function can access its own data or data belonging to parent states or the chart.
The function signature label specifies a name for your function and the formal names for its arguments and return values. A signature label has this syntax:
[return_val1,return_val2,...] = function_name(arg1,arg2,...)
You can use the same variable name for both arguments and return values. For example,
a function with this signature label uses the variables y1
and
y2
as both inputs and
outputs:
[y1,y2,y3] = f(y1,u,y2)
y1
and y2
are
passed by reference (as pointers), and u
is passed by value. Passing
inputs by reference reduces the number of times that the generated code copies
intermediate data, resulting in more optimal code.You can call truth table functions from the actions of any state or transition. You can also call truth table functions from other functions. If you export a truth table function, you can call it from any chart in the model. For more information about exporting functions, see Export Stateflow Functions for Reuse.
The syntax for a call to a truth table function is the same as the function signature, with actual arguments replacing the formal ones specified in a signature. If the data types of an actual and formal argument differ, a function casts the actual argument to the type of the formal argument.
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 Assign Values to All Elements of a Matrix.
You can set general properties for your truth table function through its properties dialog box. To open the function properties dialog box, right-click the truth table function box and select Properties from the context menu.
Function name. Click the function name link to bring your function to the foreground in its native chart.
Controls the inlining of your function in generated code:
Auto
— Determines whether to inline your function
based on an internal calculation.
Inline
— Inlines your function if you do not export
it to other charts and it is not part of a recursion. (A recursion exists if your
function calls itself directly or indirectly through another function call.)
Function
— Does not inline your function.
Signature label for your function. For more information, see Declare Function Arguments and Return Values.
Controls the level of diagnostics for underspecification in your truth table function. For more information, see Correct Overspecified and Underspecified Truth Tables.
Controls the level of diagnostics for overspecification in your truth table function. For more information, see Correct Overspecified and Underspecified Truth Tables.
Controls the action language for your Stateflow truth table function. Choose between MATLAB® or C. For more information, see Differences Between MATLAB and C as Action Language Syntax.
Function description. You can enter brief descriptions of functions in the hierarchy.
Link to online documentation for the function. You can enter a web URL address or a MATLAB command that displays documentation in a suitable online format, such as an HTML file or text in the MATLAB Command Window. When you click the Document link hyperlink, Stateflow displays the documentation.