Group Chart Objects by Using Boxes

A box is a graphical object that organizes other objects in your chart, such as functions and states. You can use a box to encapsulate states and functions in a separate namespace. Boxes are supported only in Stateflow® charts in Simulink® models.

For example, in this chart, the box Heater groups together related states Off and On.

Chart modeling a bang-bang controller.

For more information about this example, see Model Bang-Bang Temperature Control System.

Semantics of Stateflow Boxes

Visibility of Graphical Objects in Boxes

Boxes add a level of hierarchy to Stateflow charts. This property affects visibility of functions and states inside a box to objects that reside outside of the box. If you refer to a box-parented function or state from a location outside of the box, you must include the box name in the path. See Group Functions Using a Box.

Activation Order of Parallel States

Boxes affect the implicit activation order of parallel states in a chart. If your chart uses implicit ordering, parallel states within a box wake up before other parallel states that are lower or to the right in that chart. Within a box, parallel states wake up in top-down, left-right order. See Group States Using a Box.

Note

To specify activation order explicitly on a state-by-state basis, select User-specified state/transition execution order in the Chart properties dialog box. This option is selected by default when you create a new chart. For details, see Explicit Ordering of Parallel States.

Rules for Using Boxes

When you use a box, these rules apply:

  • Include the box name in the path when you use dot notation to refer to a box-parented function or state from a location outside of the box.

  • You can move or draw graphical objects inside a box, such as functions and states.

  • You can add data to a box so that all the elements in the box can share the same data.

  • You can group a box and its contents into a single graphical element. See Group States.

  • You can subchart a box to hide its elements. See Encapsulate Modal Logic by Using Subcharts.

  • You cannot define action statements for a box, such as entry, during, and exit actions.

  • You cannot define a transition to or from a box. However, you can define a transition to or from a state within a box.

Draw and Edit a Box

Create a Box

You create boxes in your chart by using the box tool shown below.

  1. Click the Box tool.

  2. Move your pointer into the drawing area.

  3. Click in any location to create a box.

    The new box appears with a question mark (?) name in its upper left corner.

  4. Click the question mark label.

  5. Enter a name for the box and then click outside of the box.

Delete a Box

To delete a box, click to select it and press the Delete key.

Examples of Using Boxes

Group Functions Using a Box

This chart shows a box named Status that groups together MATLAB® functions.

Chart execution takes place as follows:

  1. The state Cold activates first.

  2. Upon entry, the state Cold invokes the function Status.msgCold.

    This function displays a status message that the temperature is cold.

    Note

    Because the MATLAB function resides inside a box, the path of the function call must include the box name Status. If you omit this prefix, an error message appears.

  3. If the value of the input data temp exceeds 80, a transition to the state Warm occurs.

  4. Upon entry, the state Warm invokes the function Status.msgWarm.

    This function displays a status message that the temperature is warm.

    Note

    Because the MATLAB function resides inside a box, the path of the function call must include the box name Status. If you omit this prefix, an error message appears.

  5. If the value of the input data temp drops below 60, a transition to the state Cold occurs.

  6. Steps 2 through 5 repeat until the simulation ends.

Group States Using a Box

This chart shows a box named Status that groups together related states. The chart uses implicit ordering for parallel states, instead of the default explicit mode. (For details, see Implicit Ordering of Parallel States.)

The main ideas of this chart are:

  • The state Temp wakes up first, followed by the state Wind_Chill. Then, the state Monitor wakes up.

    Note

    This implicit activation order occurs because Temp and Wind_Chill reside in a box. If you remove the box, the implicit activation order changes, as shown, to: Temp, Monitor, Wind_Chill.

  • Based on the input data temp, transitions between substates occur in the parallel states Status.Temp and Status.Wind_Chill.

  • When the transition from Status.Temp.Cold to Status.Temp.Warm occurs, the transition condition in(Status.Temp.Warm) becomes true.

  • When the transition from Status.Temp.Warm to Status.Temp.Cold occurs, the transition condition in(Status.Temp.Cold) becomes true.

    Note

    Because the substates Status.Temp.Cold and Status.Temp.Warm reside inside a box, the argument of the in operator must include the box name Status. If you omit this prefix, an error message appears. For information about the in operator, see Check State Activity by Using the in Operator.