Documentation

What Is an Atomic Subchart?

In a Stateflow® chart, an atomic subchart is a graphical object that helps you reuse the same state or subchart across multiple charts and models. Atomic subcharts allow:

  • Ease of team development for people working on different parts of the same chart

  • Faster simulation after making small changes to a chart with many states or levels of hierarchy

  • Manual inspection of generated code for a specific state or subchart in a chart

  • Ability to animate and debug multiple charts side by side

States, subcharts, and atomic subcharts have these key similarities and differences:

CapabilityStateSubchartAtomic Subchart
Can behave as a standalone chartNoNoYes
Can generate reusable codeNoNoYes
Supports access to event broadcasts outside the scope of that objectYesYesNo
Supports access to data at any level of the hierarchyYesYesNo

Atomic subcharts and atomic subsystems (see Atomic Subsystem in the Simulink® documentation) have these key similarities and differences:

CapabilityAtomic SubchartAtomic Subsystem
Supports generation of reusable codeYesYes
Supports usage as a library linkYesYes
Requires parameterizing of data when used as a library linkYesNo
Supports explicit specification of sample timeNoYes

The following examples show how to use atomic subcharts for modeling typical applications:

ExampleApplication
sf_atomic_sensor_pairA redundant sensor pair
sf_elevatorAn elevator system with two identical lifts

When to Use Atomic Subcharts

Consider using atomic subcharts when one or more of these scenarios apply:

ScenarioReason for Using Atomic SubchartsReferenceTutorial

You want to reuse the same state or subchart many times across different charts or models to facilitate large-scale modeling.

You can store an atomic subchart in a library to enable reuse across different charts and models. When you change an atomic subchart in a library, the change propagates to all links.

Comparison of Modeling Methods

Reuse a State Multiple Times in a Chart

You want to use simulation to test your changes, one by one, without recompiling the entire chart.

When you modify an atomic subchart, recompilation occurs for only that object and not the entire chart.

Comparison of Simulation Methods

Reduce the Compilation Time of a Chart

You want to break a chart into standalone parts because multiple people are working on different parts of the chart.

Because atomic subcharts behave as standalone objects, people can work on different parts of a chart without affecting any work that someone else is doing.

Comparison of Editing Methods

Divide a Chart into Separate Units

You want to inspect Simulink Coder™ or Embedded Coder® generated code manually for a specific part of a chart.

You can specify that code for an atomic subchart appears in a separate file for unit testing.

Comparison of Code Generation Methods

Generate Reusable Code for Unit Testing

Benefits of Using Atomic Subcharts

Comparison of Modeling Methods

The following sections compare two ways of modeling similar states in charts.

Model without Atomic Subcharts.  You create a separate instance of each state in your chart.

In this chart, the only differences between the two states are the names of variables.

Model with Atomic Subcharts.  You create a single state and convert it to an atomic subchart, which you store in a new library. From that library, you can copy and paste the atomic subchart for use in any chart. Then update the mapping of inputs, outputs, local data, or parameters as needed.

This modeling method minimizes maintenance of similar states. When you modify the atomic subchart in the library, your changes propagate automatically to the links in all charts and models.

For more information, see Reuse a State Multiple Times in a Chart.

Comparison of Simulation Methods

The following sections compare two ways of simulating a chart.

Simulation Without Atomic Subcharts.  You make a small change to one part of a chart that contains many states or several levels of hierarchy. When you start simulation to test that change, recompilation occurs for the entire chart.

Because recompiling the entire chart takes a long time, you make several changes before testing. However, if you find an error, you must step through all your changes to identify what causes the error.

Simulation With Atomic Subcharts.  You make a small change to an atomic subchart in a chart that contains many states or several levels of hierarchy. When you start simulation to test that change, recompilation occurs only for the atomic subchart.

Incremental builds for simulation decrease the time required to recompile the chart. This reduction enables you to test each change, one by one, instead of waiting to test multiple changes. By testing each change individually, you can quickly identify a change that causes an error.

For more information, see Reduce the Compilation Time of a Chart.

Comparison of Editing Methods

The following sections compare two ways of editing a chart.

Edit Without Atomic Subcharts.  You edit one part of a chart, while someone else edits another part of the same chart. At submission time, you merge your changes.

Edit With Atomic Subcharts.  You store one part of a chart as an atomic subchart in a library. You edit that subchart separately, while someone else edits the main chart. At submission time, no merge is necessary because the changes exist in separate models.

For more information, see Divide a Chart into Separate Units.

Comparison of Code Generation Methods

The following sections compare two ways of generating code.

Code Generation Without Atomic Subcharts.  You generate code for the entire model in one file and look through that entire file to find code for a specific part of the chart.

Code Generation With Atomic Subcharts.  You specify code generation parameters so that code for an atomic subchart appears in a separate file. This method of code generation enables unit testing for a specific part of a chart. You can avoid searching through unrelated code and focus only on the part that interests you.

Note

Unreachable Stateflow states are optimized out and are not included in the generated code.

For more information, see Generate Reusable Code for Unit Testing.

Was this topic helpful?