Program a Truth Table

Open a Truth Table for Editing

After you create and label a truth table in a chart, you specify its logical behavior. Double-click the truth table function to open the Truth Table Editor.

By default, a truth table contains a Condition Table and an Action Table, each with one row. The Condition Table also contains a single decision column, D1, and a single action row.

Select An Action Language

Select the language you want to use for programming conditions and actions in your truth table:

  1. In the Truth Table Editor, select Settings > Language.

  2. Choose a language from the drop-down menu.

Enter Truth Table Conditions

Conditions are the starting point for specifying logical behavior in a truth table. You open the truth table ttable for editing in Open a Truth Table for Editing. In this topic, 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 also enter an optional description in the Description column. Use the following procedure to enter conditions for the truth table ttable:

  1. Click anywhere in the Condition Table to select it.

  2. Select Edit > Append Row twice.

    The editor appends two rows to the bottom of the Condition Table.

  3. Click and drag the bar that separates the Condition Table and the Action Table panes down to enlarge the Condition Table pane.

  4. In the Condition Table, click the top cell of the Description column.

    A flashing text cursor appears in the cell, which appears highlighted.

  5. Enter the following text:

    x is equal to 1

    Condition descriptions are optional, but appear as comments in the generated code for the truth table.

  6. Press the Tab key to select the next cell on the right in the Condition column.

      Tip   You can use Shift+Tab to select the next cell on the left.

  7. In the first cell of the Condition column, enter the following text:

    XEQ1:

    This text is an optional label 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 (_).

  8. Press Enter and enter the following text:

    x == 1

    This text is the actual condition. Each condition you enter must evaluate to zero (false) or nonzero (true). You can use optional brackets in the condition (for example, [x == 1]) as you do in transition labels.

    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.

  9. 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:

  1. Click anywhere in the Condition Table to select it.

  2. Select Edit > Append Column three times to add three columns to the right end of the Condition Table.

  3. Click the top cell in decision column D1.

    A flashing text cursor appears in the cell, which appears highlighted.

  4. Press the space bar until a value of T appears.

    Pressing the space bar toggles through the possible values of F, -, and T. You can also enter these characters directly. The editor rejects all other entries.

  5. Press the down arrow key to advance to the next cell down in the D1 column.

    In the decision columns, you can use the arrow keys to advance to another cell in any direction. You can also use Tab and Shift+Tab to advance left or right in these cells.

  6. 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 to the left. You enter a default decision as the last decision column on the right with an entry of - for all conditions in the decision. This entry represents any outcome for the condition, T or F.

In the preceding example, the default decision column, D4, specifies these decisions:

Condition

Decision 4

Decision 5

Decision 6

Decision 7

Decision 8

x == 1

F

T

F

T

T

y == 1

F

F

T

T

T

z == 1

F

T

T

F

T

    Tip   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 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 Editor. 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.

This section describes how to program truth table actions with these topics:

Set Up the Action Table

  1. Click anywhere in the Action Table to select it.

  2. Select Edit > Append Row three times to add three rows to the bottom of the Action Table:

  3. Program the actions using the language you selected for the truth table.

    If you selected...Use this procedure...
    CProgram Actions Using C Expressions
    MATLABProgram Actions Using MATLAB Expressions

Program Actions Using C Expressions

Follow this procedure to program your actions using C as the action language:

  1. Click the top cell in the Description column of the Action Table.

    A flashing text cursor appears in the cell, which appears highlighted.

  2. Enter the following description:

    set t to 1
    

    Action descriptions are optional, but appear as comments in the generated code for the truth table.

  3. Press Tab to select the next cell on the right, in the Action column.

  4. 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 (_).

  5. Press Enter and enter the following text:

    t=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, t=1, sets the value of the return value t. 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.

      Tip   If you omit the semicolon at the end of an action, the result of the action echoes to the MATLAB Command Window when the action executes during simulation. Use this echoing option as a debugging tool.

  6. Enter the remaining actions in the Action Table, as shown:

Now you are ready to assign actions to decisions, as described in Assign Truth Table Actions to Decisions.

Program Actions Using MATLAB Expressions

If you selected 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 flows

  • for loop

Follow these steps:

  1. Click the top cell in the Description column of the Action Table.

    A flashing text cursor appears in the cell, which appears highlighted.

  2. Enter this description:

    Maintain a counter and a circular 
    vector of values of length 6.
    Every time this action is called, 
    output t takes the next value of 
    the vector.
    

    Action descriptions are optional, but appear as comments in the generated code for the truth table.

  3. Press Tab to select the next cell on the right, in the Action column.

  4. 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 (_).

  5. Press Enter and enter the following text:

    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 t takes the next value in values vector
    t = 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 t equal to the next value of the vector values. 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.

      Note:   If you omit the semicolon at the end of an action, the result of the action echoes to the MATLAB Command Window when the action executes during simulation. Use this echoing option as a debugging tool.

  6. Enter the remaining actions in the Action Table, as shown:

    If action A1 executes during simulation, a plot of the values vector appears:

Now you are ready to assign actions to decisions, as described in Assign Truth Table Actions to Decisions.

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 or 1;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:

  1. Click the bottom cell in decision column D1, the first cell of the Actions row of the Condition Table.

  2. Enter the action specifier A1 for decision column D1.

    When D1 is true, action A1 in the Action Table executes.

  3. 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.

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:

  1. In the Truth Table Editor, right-click row 1 of the Action Table and select Insert Row.

    A blank row appears at the top of the Action Table.

  2. Select Edit > Append Row.

    A blank row appears at the bottom of the Action Table.

  3. Click and drag the bottom border of the Truth Table Editor to show all six rows of the Action Table:

  4. Add the initial action in row 1 as follows:

    Truth Table TypeDescriptionAction
    C

    Initial action:

    Display message

    INIT:

    ml.disp('truth table ttable entered');

    MATLAB

    Initial action:

    Display message

    INIT:

    coder.extrinsic('disp');

    disp('truth table ttable entered');

  5. Add the final action in row 6 as follows:

    Truth Table TypeDescriptionAction
    C

    Final action:

    Display message

    FINAL:

    ml.disp('truth table ttable exited');

    MATLAB

    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.

Was this topic helpful?