Reuse a State Multiple Times in a Chart

An atomic subchart is a graphical object that helps you to create standalone subcomponents in a Stateflow{R} chart. Atomic subcharts are supported only in Stateflow charts in Simulink® models. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Goal of the Tutorial

In this model, the top Sine Wave block uses a frequency of 1 radian per second, and the bottom Sine Wave block uses a frequency of 2 radians per second. The blocks use the same amplitude (1) and phase shift (0).

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 input and output data.

Simulating the model produces these results.

Suppose that you want to reuse the contents of state A in the chart. You can convert that state to an atomic subchart and then use multiple linked instances of that subchart in your chart.

Edit a Model to Use Atomic Subcharts

By replacing states in your chart with atomic subcharts, you can reuse the same object in your model and retain the same simulation results. To skip the conversion steps and access the new model, click here.

Step 1: Convert a State to an Atomic Subchart

To convert state A to an atomic subchart, right-click the state and select Group & Subchart > Atomic Subchart. State A changes to an atomic subchart.

Step 2: Create a Library for the Atomic Subchart

To enable reuse, store the atomic subchart in a library:

  1. Create a new library model.

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

  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. This independent property enables you to reuse the contents of the atomic subchart.

Step 3: Replace States with Linked Atomic Subcharts

To replace the states in your chart with linked atomic subcharts:

  1. Delete both states from the chart.

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

  3. Rename the second instance as 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