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:
|Can behave as a standalone chart||No||No||Yes|
|Can generate reusable code||No||No||Yes|
|Supports access to event broadcasts outside the scope of that object||Yes||Yes||No|
|Supports access to data at any level of the hierarchy||Yes||Yes||No|
Atomic subcharts and atomic subsystems (see Atomic Subsystem in the Simulink® documentation) have these key similarities and differences:
|Capability||Atomic Subchart||Atomic Subsystem|
|Supports generation of reusable code||Yes||Yes|
|Supports usage as a library link||Yes||Yes|
|Requires parameterizing of data when used as a library link||Yes||No|
|Supports explicit specification of sample time||No||Yes|
The following examples show how to use atomic subcharts for modeling typical applications:
|A redundant sensor pair|
|An elevator system with two identical lifts|
Consider using atomic subcharts when one or more of these scenarios apply:
|Scenario||Reason for Using Atomic Subcharts||Reference||Tutorial|
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.
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.
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.
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.
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.
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.
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.
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.
Unreachable Stateflow states are optimized out and are not included in the generated code.
For more information, see Generate Reusable Code for Unit Testing.