Reuse a State Multiple Times in a Chart

This example shows how to use linked atomic subcharts to repeat the same configuration of states and trasitions multiple times in a Stateflow® chart. Atomic subcharts are supported only in Stateflow charts in Simulink® models. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Original Model Without Atomic Subcharts

This model contains two Sine Wave blocks: one with a frequency of 1 radian per second, and the other with a frequency of 2 radians per second.

In the chart, each state uses saturator logic to convert the input sine wave to an output square wave of the same frequency. The states perform the same actions and differ only in the names of their input and output data.

Simulating the model produces these results.

Because this example does not use atomic subcharts, you have to maintain each subcomponent manually. For example, if you change the logic in state A, then you must make the same change in state B.

In contrast, if you replace the states in this example with atomic subcharts, you can reuse the same object in your model and retain the same simulation results. You can save state A as an atomic subchart in a library model and then use multiple linked instances of that subchart in your chart. Changes in the library model propagate to all linked instances of the subchart.

Edit Model to Use Atomic Subcharts

Step 1: Convert a State to an Atomic Subchart

Right-click state A and select Group & Subchart > Atomic Subchart. State A changes to an atomic subchart and displays the label Atomic in the upper-left corner.

Step 2: Create a Library for the Atomic Subchart

  1. Create a new library model.

  2. Copy the atomic subchart and paste it in your library model.

  3. Save your library model.

In the library model, the atomic subchart appears as an independent chart with an input port and an output port.

Step 3: Replace States with Linked Atomic Subcharts

  1. Delete both states in the chart.

  2. Copy the atomic subchart in your library and paste it in your chart twice.

  3. Change the name of the second atomic subchart to B.

Each linked atomic subchart appears opaque and contains the label Link in the upper-left corner.

Step 4: Edit the Mapping of Input and Output Variables

If you simulate the model now, the output for y2 is zero. You also see warnings about unused data. These warnings appear because atomic subchart B uses u1 and y1 instead of u2 and y2.

To fix these warnings, you must edit the mapping of input and output variables.

  1. Right-click subchart B and select Subchart Mappings.

  2. Under Input Mapping, specify the main chart symbol for u1 to be u2.

  3. Under Output Mapping, specify the main chart symbol for y1 to be y2.

  4. Click OK.

Run the New Model

When you simulate the new model, the results match those of the original design.

Propagate a Change in the Library Chart

Suppose that, in the library chart, you edit the transition from Pos to Neg.

This change propagates to all linked atomic subcharts in your main chart. You do not have to update each state individually.

See Also

Related Topics