Main Content

Build State Transition Tables Programmatically

R2026b

To build or modify a state transition table (STT) programmatically, use the Stateflow® API. The API provides three objects that represent the structure of a state transition table:

Use these objects to add, configure, and delete rows, columns, and cells without opening the STT editor.

Access the state transition table object

To access an STT in a loaded model, use find on the Stateflow root object. Filter by Stateflow.StateTransitionTableChart to return only STT objects and exclude charts.

load_system("myModel");
stt = find(sfroot, "-isa", "Stateflow.StateTransitionTableChart", ...
    Name="MySTT");

To access all STTs in a model, omit the Name filter.

allSTTs = find(sfroot, "-isa", "Stateflow.StateTransitionTableChart");

Add state rows

To add a state row to the table, call addStateRow on the Stateflow.StateTransitionTableChart object. The method appends the row to the end of the table and returns a Stateflow.StateTransitionTableRow object. Set the Name property to assign a state name.

row = stt.addStateRow();
row.Name = "Idle";

To add a child state row nested under an existing row, call addChildStateRow on the parent row.

childRow = row.addChildStateRow();
childRow.Name = "Idle_Low";

To add a sibling row at the same level as an existing row, call addStateRowBelow on the row.

siblingRow = row.addStateRowBelow();
siblingRow.Name = "Active";

Configure state actions

To set state actions, write to the LabelString property of the row. Use the standard Stateflow label syntax with en:, du:, and ex: prefixes.

row.LabelString = "Idle" + newline + "en: speed = 0;";

The EntryAction, DuringAction, ExitAction, and OnAction properties return the parsed action strings derived from LabelString. These properties are read-only.

row.EntryAction   % returns 'speed = 0;'

Set decomposition and default state

To set the decomposition type of the table or a row, write to the Decomposition property. Valid values are EXCLUSIVE_OR and PARALLEL_AND.

stt.Decomposition = "EXCLUSIVE_OR";

To mark a state row as the default state, set its IsDefaultState property to true. The following constraints apply:

  • Default transition path rows and inner transition path rows cannot be set as the default state.

  • A single child state row cannot be unset as the default state.

  • A state row whose parent already has a default transition path row cannot be set as the default state.

  • A state row in a parallel decomposition context cannot be set as the default state.

row.IsDefaultState = true;

Add transition columns and configure cells

To add a transition column to the table, call addTransitionColumn. The method appends the column and returns an array of Stateflow.StateTransitionTableCell objects, one per row in the table.

cells = stt.addTransitionColumn();

To access the cells of an existing column by name or index, call getTransitionColumn.

ifCells = stt.getTransitionColumn("IF");
elseIfCells = stt.getTransitionColumn(2);

To set the condition, action, and destination of a cell, write to the Condition, Action, and Destination properties of the cell object. Set Destination to the name of a state row.

cell = cells(1);
cell.Condition = "speed > 10";
cell.Action = "mode = 1;";
cell.Destination = "Active";

To delete a transition column, call deleteTransitionColumn by name or index.

stt.deleteTransitionColumn("ELSE-IF(2)");

Add structural rows

To add a default transition path row at the chart level, call addDefaultTransitionPathRow on the table object. To add a default or inner transition path row under a specific state row, call addDefaultTransitionPathRow or addInnerTransitionPathRow on the row. Adding a default transition path row clears any existing default transitions at that level.

dtpRow   = stt.addDefaultTransitionPathRow();
dtpRow   = row.addDefaultTransitionPathRow();
innerRow = row.addInnerTransitionPathRow();

Find and inspect STT objects

To find objects within an STT by property, call find on the table or row object. The STT hierarchy follows these containment rules:

  • A table directly contains rows.

  • A row contains child rows and transition cells.

Calling find on the table returns all rows and cells in the table. Calling it on a row returns all child rows and cells for that row. Use the -isa argument to filter by object type and the -depth argument to limit search depth.

targetRow = stt.find("-isa", "Stateflow.StateTransitionTableRow", ...
    Name="Idle");

rowCells = row.find("-isa", "Stateflow.StateTransitionTableCell", ...
    "-depth", 1); 

To get all top-level rows without searching by property, call getRows on the table. This method returns state rows, default transition path rows, and inner transition path rows. To get a specific top-level state row by name, call getRow. The getRow method does not return default transition path rows or inner transition path rows.

allRows = stt.getRows();
idleRow = stt.getRow("Idle");

To get the transition cells for a specific row, call getTransitionCell on the row.

cells = row.getTransitionCell();

The getChildren method returns internal chart objects, not STT rows. To retrieve rows, use getRows or find. Calling getChildren on a row object works correctly and returns child rows and cells.

Navigate the object hierarchy

To get the parent of a row, call getParent on the row. The method returns either the table object or another row object, depending on the level of the row in the hierarchy. To get the parent of a cell, call getParent on the cell. The method returns the row that contains the cell.

parent      = row.getParent();
rowFromCell = cell.getParent();

Delete rows

To delete a row from the table, call deleteRow on the row object.

row.deleteRow();

The table requires at least one state row. Calling deleteRow on the last remaining row returns an error and no-ops the operation.

Validate the table

To open the STT editor and verify the table structure visually, call view on the table object.

stt.view();

See Also

| | |

Topics