Main Content

Use Truth Tables to Model Combinatorial Logic

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 not supported in standalone Stateflow®charts in MATLAB®.

You can add aTruth Tableblock directly to your Simulink®model, or you can define a truth table function in a Stateflow chart, state, or subchart.Truth Tableblocks 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 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 anywhere in a chart, put your truth table function at the chart level.

  • 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, seeExport Stateflow Functions for Reuse.

Note

真值表函数可以访问表和状态data above it in the Stateflow hierarchy.

Layout of a Truth Table

This truth table function has the namettable. It takes three arguments (x,y, andz) 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 theConditioncolumn 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 withDecision 1. TheDecision 4covers 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 conditionsx == 1andy == 1are false and conditionz == 1is true, thenDecision 3is true and the variableris 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)执行。此表列出了pseudocode corresponding to the evaluation of this truth table example.

Pseudocode

Description

if ((x == 1) & !(y == 1) & !(z == 1)) r = 1;

IfDecision 1is true, then setr=1.

elseif (!(x == 1) & (y == 1) & !(z == 1)) r = 2;

IfDecision 2is true, then setr=2.

elseif (!(x == 1) & !(y == 1) & (z == 1)) r = 3;

IfDecision 3is true, then setr=3.

else r = 4; endif

If all other decisions are false, then default decision is true. Setr=4.

Define a Truth Table Function

To define a truth table function:

  1. In the object palette, click the truth table function icon.

  2. On the chart canvas, click the location for the new truth table function.

  3. Enter the signature label for the function.

    The signature label of the function 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 specify multiple return values and multiple input arguments. Each return value and input argument can be a scalar, vector, or matrix of values. For functions with only one return value, omit the brackets in the signature label.

    You can use the same variable name for both arguments and return values. For example, a function with this signature label uses the variablesy1andy2as both inputs and outputs:

    [y1,y2,y3] = f(y1,u,y2)
    If you export this function to C code,y1andy2are passed by reference (as pointers), anduis passed by value. Passing inputs by reference reduces the number of times that the generated code copies intermediate data, resulting in more optimal code.

    Note

    Do not use the name of a chart symbol as a function argument or return value. For example, if eitherxoryare chart data, defining a function with the signaturey = f (x)results in a run-time error.

  4. To program the function, open the truth table editor by double-clicking the function box.

  5. In the truth table editor, add conditions, decisions, and actions. For more information, seeProgram a Truth Table.

  6. In the Model Explorer, expand the chart object and select the truth table function. The arguments and return values of the function signature appear as data items that belong to your function. Arguments have the scopeInput. Return values have the scopeOutput.

  7. In the Data properties dialog box for each argument and return value, specify the data properties, as described inSet Data Properties.

  8. Create any additional data items required by your function. For more information, seeAdd Data Through the Model Explorer.

    Your function can access its own data or data belonging to parent states or the chart. The data items in the function can have one of these scopes:

    • Constant— Constant data retains its initial value through all function calls.

    • Parameter— Parameter data retains its initial value through all function calls.

    • Local— Local data persists from one function call to the next function call. Valid only for truth tables that use C as the action language.

    • Temporary— Temporary data initializes at the start of every function call. Valid only for truth tables that use C as the action language.

    In truth table functions that use C as the action language, define local data when you want your data values to persist across function calls throughout the simulation. Define temporary data when you want to initialize data values at the start of every function call. For example, you can define a counter withLocalscope if you want to track the number of times that you call the function. In contrast, you can designate a loop counter to haveTemporaryscope if you do not need the counter value to persist after the function completes.

    In truth table functions that use MATLAB as the action language, you do not need to define local or temporary function data. If you use an undefined variable, Stateflow creates a temporary variable that is available to the rest of the function. To store values that persist across function calls, your function can access local data at the chart level.

    Tip

    You can initialize local and temporary data in your function from the MATLAB workspace. For more information, seeInitialize Data from the MATLAB Base Workspace.

Call Truth Table Functions in States and Transitions

You can call truth table functions from the actions of any state or transition or 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, seeExport Stateflow Functions for Reuse.

To call a truth table function, use the function signature and include an actual argument value for each formal argument in the function signature.

[return_val1,return_val2,...] = function_name(arg1,arg2,...)

如果实际的数据类型和正式arguments differ, the function casts the actual argument to the type of the formal argument.

Specify Properties of Truth Table Functions

You can specify properties for a truth table function in the Model Explorer or the Truth Table properties dialog box.

  • To use the Model Explorer:

    1. To open the Model Explorer, in theModelingtab, selectModel Explorer.

    2. In theModel Hierarchypane, select the truth table function.

    3. In theTruth Tablepane, edit the truth table function properties.

  • To use the Truth Table properties dialog box:

    1. In the Stateflow Editor, right-click the truth table function.

    2. SelectProperties.

    3. Edit the truth table function properties.

You can also specify truth table function properties programmatically by usingStateflow.TruthTableobjects. For more information about the Stateflow programmatic interface, seeOverview of the Stateflow API.

Name

Function name. Click the function name link to bring your function to the foreground in its native chart.

Function Inline Option

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.

Label

Signature label for your function. The function signature label specifies a name for your function and the formal names for its arguments and return values.

Underspecification

Controls the level of diagnostics for underspecification in your truth table function. For more information, seeCorrect Overspecified and Underspecified Truth Tables.

Overspecification

Controls the level of diagnostics for overspecification in your truth table function. For more information, seeCorrect Overspecified and Underspecified Truth Tables.

Action Language

Controls the action language for your Stateflow truth table function. Choose between MATLAB or C. For more information, seeDifferences Between MATLAB and C as Action Language Syntax.

Description

Function description. You can enter brief descriptions of functions in the hierarchy.

Document Link

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 theDocument linkhyperlink, Stateflow displays the documentation.

Specify Properties for Truth Table Blocks

To open the Truth Table properties dialog box, in theModelingtab, clickTable Properties.

The properties forTruth Tableblocks are a combination of properties of truth table functions and charts that use MATLAB as the action language. For a description of each property, seeSpecify Properties of Truth Table FunctionsandSpecify Properties for Stateflow Charts.

You can also specifyTruth Tableblock properties programmatically by usingStateflow.TruthTableChartobjects. For more information about the Stateflow programmatic interface, seeOverview of the Stateflow API.

See Also

Related Topics