Create Reusable Subcomponents by Using Atomic Subcharts
An atomic subchart is a graphical object that helps you to create independent subcomponents in a Stateflow® chart. Atomic subcharts are not supported in standalone Stateflow charts in MATLAB®.
Atomic subcharts allow for:
Reuse of the same state or subchart across multiple charts and models
Faster simulation after making small changes to a chart with many states or levels of hierarchy
Ease of team development when multiple people are working on different parts of the same chart
Manual inspection of generated code for a specific state or subchart in a chart
An atomic subchart looks opaque and includes the label Atomic in the upper-left corner. If you use a linked atomic subchart from a library, the label Link appears in the upper-left corner.
Example of an Atomic Subchart
This example illustrates the difference between a normal subchart and an atomic subchart.
In the Air Controller chart,
PowerOff is a normal subchart.
PowerOn is an atomic subchart. Both subcharts look opaque, but the
PowerOn includes the label Atomic on the upper-left corner.
Benefits of Using Atomic Subcharts
Behave as independent charts.
Support usage as library links.
Support the generation of reusable code.
Allow mapping of inputs, outputs, parameters, data store memory, and input events.
Atomic subcharts do not support access to:
Data at every level of the chart hierarchy.
Event broadcasts outside the scope of the atomic subchart.
Atomic subcharts do not support explicit specification of sample time.
Create an Atomic Subchart
You can create an atomic subchart by converting an existing state or subchart or by linking a chart from a library model. After creating the atomic subchart, update the mapping of variables by right-clicking the atomic subchart and selecting Subchart Mappings. For more information, see Map Variables for Atomic Subcharts and Boxes.
Convert a State or Normal Subchart to an Atomic Subchart
To create an independent component that allows for faster debugging and code generation workflows, convert an existing state or subchart into an atomic subchart. In your chart, right-click a state or a normal subchart and select Group & Subchart > Atomic Subchart. The label Atomic appears in the upper-left corner of the subchart.
The new atomic subchart has a copy of every data object that the subchart accesses in the chart. Local data is copied as data store memory. The scope of other data, including input and output data, does not change.
Converting a state or subchart to an atomic subchart automatically replaces any supertransition into or out of the state or subchart with a transition that connects to an entry or exit port. Entry and exit ports enable your chart to transition across boundaries in the Stateflow hierarchy while isolating the logic for entering and exiting the atomic subchart. For more information, see Create Entry and Exit Connections Across State Boundaries and Robot Trajectory Planning with Reusable Components.
For a list of issues that can prevent you from converting a state or subchart to an atomic subchart, see Restrictions for Converting to Atomic Subcharts.
Link an Atomic Subchart from a Library
To create a subcomponent for reuse across multiple charts and models, create a link from a library model. Copy a chart in a library model and paste it to a chart in another model. If the library chart contains any states, it appears as a linked atomic subchart with the label Link in the upper-left corner.
This modeling method minimizes maintenance of similar states. When you modify the atomic subchart in the library, your changes propagate to the links in all charts and models.
If the library chart contains only functions and no states, then it appears a linked atomic box in the chart. For more information, see Reuse Functions by Using Atomic Boxes.
Convert an Atomic Subchart to a Normal Subchart
Converting an atomic subchart back to a state or a normal subchart removes all of its variable mappings. The conversion merges subchart-parented data objects with the chart-parented data to which they map.
If the atomic subchart is a library link, right-click the atomic subchart and select Library Link > Disable Link.
To convert an atomic subchart back to a normal subchart, right-click the atomic subchart and clear the Group & Subchart > Atomic Subchart check box.
To convert the subchart back to a state, right-click the subchart and clear the Group & Subchart > Subchart check box.
If necessary, rearrange graphical objects in your chart.
You cannot convert an atomic subchart to a normal subchart if:
The atomic subchart maps a parameter to an expression other than a single variable name. For example, mapping a parameter
data1to one of these expressions prevents the conversion of an atomic subchart to a normal subchart:
data2 + 3
Both of these conditions are true:
The atomic subchart contains MATLAB functions or truth table functions that use MATLAB as the action language.
The atomic subchart does not map each variable to a variable of the same name in the main chart.
When to Use Atomic Subcharts
Reuse State Logic
Suppose that you want to reuse the same state or subchart many times to facilitate large-scale modeling.
If you do not use atomic subcharts, you have to maintain each copy of a
subcomponent manually. For example, this chart contains two states with a
similar structure. The only difference between the two states is the names of
variables. If you change the logic in state
A, then you must
make the same change in state
To enable reuse of subcomponents by using linked atomic subcharts, create a
single copy of state
A and store it as a chart in a library
model. From that library, copy and paste the atomic subchart twice in your
chart. Then update the mapping of subchart variables as needed.
When you change an atomic subchart in a library, the change propagates to all library links. For more information, see Reuse a State Multiple Times in a Chart.
Debug Charts Incrementally
Suppose that you want to test a sequence of changes in a chart that contains many states or several levels of hierarchy.
If you do not use atomic subcharts, when you make a small change to one part of a chart and start simulation, recompilation occurs for the entire chart. Because recompiling the entire chart can take a long time, you decide to make several changes before testing. However, if you find an error, you must step through all of your changes to identify the cause of the error.
In contrast, when you modify an atomic subchart, recompilation occurs for only the subchart and not for the entire chart. Incremental builds for simulation require less time to recompile. This reduction in compilation time enables you to test each individual change instead of waiting to test multiple changes at once. 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.
Develop Charts Used by Multiple People
Suppose that you want to break a chart into subcomponents because multiple people are working on different parts of the chart.
Without atomic subcharts, only one person at a time can edit the model. If someone edits one part of a chart while someone else edits another part of the same chart, you must merge those changes at submission time.
In contrast, you can store different parts of a chart as linked atomic subcharts. Because atomic subcharts behave as independent objects, different people can work on different parts of a chart without affecting the other parts of the 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.
Inspect Generated Code
Suppose that you want to inspect code generated by Simulink® Coder™ or Embedded Coder® manually for a specific part of a chart.
If you do not use atomic subcharts, you generate code for an entire model in one file. To find code for a specific part of the chart, you have to look through the entire file.
In contrast, you can specify that the 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 avoid searching through unrelated code and focus only on the code that interests you. For more information, see Generate Separate Code for an Atomic Subchart.
Atomic Subsystem (Simulink)