Documentation Center

  • Trial Software
  • Product Updates

Build Model with Stateflow Truth Table

Methods for Adding Truth Tables to Simulink Models

Methods for adding a Stateflow® truth table to a Simulink® model are:

ProcedureAction Languages SupportedHow To Do It
Add a Truth Table block directly to the model.MATLAB® only See the Truth Table block reference page.
Add a Stateflow block that calls a truth table function.C and MATLABSee Add a Stateflow Block that Calls a Truth Table Function.

Add a Stateflow Block that Calls a Truth Table Function

This section describes how to add a Stateflow block to your Simulink model, and then create a chart that calls a truth table function. These topics include:

Once you build a model in this section, finish it by programming the truth table with its behavior in Program a Truth Table.

Create a Simulink Model

To execute a truth table, you first need a Simulink model that calls a Stateflow block. Later, you will create a Stateflow chart for the Stateflow block that calls a truth table function. In this exercise, you create a Simulink model that calls a Stateflow block.

  1. At the MATLAB prompt, enter the following command:

    sfnew
    

    An untitled model with a Stateflow block appears.

  2. Click and drag the Stateflow block to the center of the model window.

  3. In the model window, select View > Library Browser.

    The Simulink Library Browser window opens with the Simulink node expanded.

  4. Under the Simulink node, select the Sources library.

    The right pane of the Simulink Library Browser window displays the blocks of the Sources library.

  5. From the right pane of the Simulink Library Browser, click and drag the Constant block to the left of the Chart block in the model.

  6. Add two more Constant blocks to the left of the Chart block, and add a Display block (from the Sinks library) to the right of the Chart block.

  7. In the model, double-click the middle Constant block.

  8. In the Block Parameters dialog box that appears, change Constant value to 0.

  9. Click OK to close the dialog box.

  10. In the model, double-click the bottom Constant block.

  11. In the Block Parameters dialog box that appears, change Constant value to 0.

  12. Click OK to close the dialog box.

    Your model should now look something like this:

  13. Open the Model Configuration Parameters dialog box.

  14. On the Solver pane, set:

    • Type to Fixed-step

    • Solver to discrete (no continuous states)

    • Fixed-step size to 1

  15. Click OK to accept these values and close the Model Configuration Parameters dialog box.

  16. Save the model as ex_first_truth_table.

Create a Stateflow Truth Table

You created a Simulink model in Create a Simulink Model that contains a Stateflow block. Now you need to open the chart for the block and specify a truth table for it:

  1. In your model, double-click the Chart block to open an empty chart.

  2. Click the Truth Table drawing tool:

  3. Move your pointer into the empty chart and notice that it appears in the shape of a box.

  4. Click to place a new truth table.

    A shaded box appears with the title truthtable.

  5. Enter the signature label

    t = ttable(x,y,z)
    

    and click outside the truth table box.

    The signature label of the truth table function follows 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.

      Note:   For functions with only one return value, you can omit the brackets in the signature label.

Specify Properties of Truth Table Functions in Stateflow Charts

After you add a truth table function to a chart, you can specify its properties by following these steps:

  1. Right-click the truth table function box and select Properties from the context menu.

    The Truth Table properties dialog box for the truth table function appears. It contains a General tab and a Documentation tab.

    The fields in the Truth Table properties dialog box under the General tab are as follows:

    Field

    Description

    Name

    Function name; read-only; click this hypertext link to bring the truth table function to the foreground in its native Stateflow chart.

    Breakpoints

    Select Function Call to set a breakpoint to pause execution during simulation when the truth table function is called.

    Function Inline Option

    This option controls the inlining of the truth table function in generated code through the following selections:

    • Auto
      Decides whether or not to inline the truth table function based on an internal calculation.

    • Inline
      Inlines the truth table function as long as it is not exported to other charts and is not part of a recursion. A recursion exists if the function calls itself either directly or indirectly through another called function.

    • Function
      Does not inline the function.

    Label

    You can specify the signature label for the function through this field. See Create a Stateflow Truth Table for more information.

    The fields in the Truth Table properties dialog box under the Documentation tab are as follows:

    Field

    Description

    Description

    Textual description/comment.

    Document link

    Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit/spec/data/speed.txt.

  2. Click OK to close the dialog box.

Call a Truth Table in a Stateflow Action

In Create a Stateflow Truth Table, you created the truth table function ttable with the signature:

t = ttable(x,y,z)

Now you need to specify a call to the truth table function in the chart. Later, when the chart executes during simulation, it calls the truth table.

You can call truth table functions from the actions of any state or transition. You can also call truth tables from other functions, including graphical functions and other truth tables. Also, if you export a truth table, you can call it from any chart in the model.

To call the ttable function from the default transition of its own chart, follow these steps:

  1. From the toolbar, select the Default Transition tool:

  2. Move your pointer to a location left of the truth table function and notice that it appears in the shape of a downward-pointing arrow.

  3. Click to place a default transition into a terminating junction.

  4. Click the question mark character (?) that appears on the default transition.

    A blinking cursor in a text field appears for entering the label of the default transition.

  5. Enter the text

    {d = ttable(a,b,c);}
    

    and click outside the transition label to finish editing it.

    You might want to adjust the label's position by clicking and dragging it to a new location. The finished chart should look something like this:

    The label on the default transition provides a condition action that calls the truth table with arguments and a return value. When the Simulink model triggers the Stateflow block during simulation, the default transition executes and a call to the truth table ttable occurs.

    The function call to the truth table must match the truth table signature. The type of the return value d must match the type of the signature return value t, and the type of the arguments a, b, and c must match the type of the signature arguments x, y, and z. You ensure this with a later step in this section when you create the data that you use in the chart.

      Tip   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 How Scalar Expansion Works for Functions.

  6. Save the model.

Create Truth Table Data in Stateflow Charts and Simulink Models

When you create a truth table with its own signature, you specify data for it in the form of a return value (t) and argument values (x, y, z). When you specify a call to a truth table, as you did in Call a Truth Table in a Stateflow Action, you specify data that you pass to the return and argument values of the truth table (d, a, b, and c). Now you must define this data for the chart as follows:

  1. Double-click the truth table function to open the Truth Table Editor.

  2. In the Truth Table Editor, select Add > Edit Data/Ports.

    The Model Explorer appears.

    In the Model Hierarchy pane, the node for the function ttable appears highlighted, and the Contents pane displays the output (t) and inputs (x, y, z) for ttable. By default, these data are scalars of type double. If you want to redefine these data with a different size and type, you do it in the Model Explorer. However, no changes are necessary for this example.

    Notice also in the Model Hierarchy pane that the node above the function ttable is Chart, the name of the chart that contains the truth table ttable.

     How do I enable the third pane in the Model Explorer?

  3. In the Model Hierarchy pane, select Chart.

    Notice that Chart contains no input or output data in the Contents pane. You must add the return and argument data for calling ttable.

  4. Select Add > Data.

    A scalar data is added to the chart in the Contents pane of the Model Explorer with the default name data. This data matches argument x in type and size.

     How do I verify type and size?

  5. In the Contents pane, double-click the entry data in the Name column.

    A small text field opens with the name data highlighted.

  6. In the text field, change the name to a.

  7. Under Scope, click the entry Local .

    A drop-down menu of selectable scopes appears with Local selected.

  8. Select Input.

    The scope Input means that the Simulink model provides the value for this data, which passes to the chart through an input port on the Stateflow block.

    You should now see the new data input a in the Contents pane.

  9. Repeat steps 4 through 8 to add the data b and c with the scope Input, and data d with the scope Output.

    The scope Output means that the chart provides this data, which passes to the Simulink model through an output port on the Stateflow block.

    You should now see the input and output data in the Model Explorer.

    The data a, b, c, and d match their counterparts x, y, z, and t in the truth table signature in size (scalar) and type (double), but have sources outside the Stateflow block. Notice that input ports for a, b, and c, and an output port for d appear on the Stateflow block in the model.

  10. Complete connections to the Simulink blocks:

  11. Save the model.

Was this topic helpful?