| Contents | Index |
| On this page… |
|---|
Rationale for Using Atomic Subcharts |
Suppose that you have a library model that contains a set of utility functions for use in multiple charts in a model. The utility functions reside in the library model to enable easier configuration management.
Models that use these utility functions can appear as referenced blocks in a top model. However, when the utility functions are exported graphical functions of a Stateflow chart, you can use only one instance of that referenced block per top model. For a complete list of model referencing limitations, see Limitations on All Model Referencing in the Simulink documentation.
With atomic subcharts, you can avoid the limitation due to exported graphical functions. You can reuse models with utility functions multiple times as referenced blocks in a top model.
To reuse utility functions across multiple models:
Create a library model with a chart that contains the utility function you want to reuse.
Create a separate model with multiple charts.
In each chart that calls the utility function, add a linked atomic subchart.
Write each call to the utility function using the full path:
linked_subchart_name.utility_function_name
Using the full path for the function call has the following advantages:
Makes clear the dependency on the utility function in the linked atomic subchart
Avoids pollution of the global namespace
Does not affect efficiency of the generated code
Reuse that model multiple times as referenced blocks in a top model.
Because there are no exported graphical functions in the charts, you can use more than one instance of that referenced block in the top model.
Suppose that you want to reuse a timer function that returns the simulation time. The following procedure shows how you can:
Call the timer function from multiple locations in a model.
Reuse that model multiple times in another model.
Store the timer function you want to reuse in a library model.
Create a new library named libTimerUtils.
Add a chart named TimerUtils to the library:

In your chart, add the following graphical function:

The utility function GetTime returns one output tout that corresponds to simulation time t. For more information about literal symbols you can use in your chart, see Supported Symbols in Actions.
Save libTimerUtils.
Develop a separate model with multiple charts that use the timer function.
Create a new model named ex_timer_function_calls.
Add two charts, Chart1 and Chart2, to the model.
In each chart, add two states, two transitions, and a linked atomic subchart:

To add the linked atomic subchart, copy the TimerUtils library chart and paste it below state A. Name the linked atomic subchart as Time.
In Chart1, add the following state action and transition condition:

Upon entry to state A, the call to GetTime returns the simulation time. The transition from state A to B occurs when more than 5 seconds of simulation time passes.
In Chart2, add the following state action and transition condition:

Upon entry to state A, the call to GetTime returns the simulation time. The transition from state A to B occurs when more than 7 seconds of simulation time passes.
In each chart, add local data with the following properties:
| Property | Value |
|---|---|
| Name | t0 |
| Scope | Local |
| Type | double |
In each chart, open the State properties dialog box for B and select Output State Activity.
This step adds an output data named B that is Boolean. The value is 1 when state B is active and 0 otherwise. For more information, see Outputting State Activity to a Simulink Model.
In your model, add two Outport blocks, Out1 and Out2. Then connect each block to the corresponding output of each chart.
Your model should look something like this:

Configure your model to meet referencing requirements:
Open the Configuration Parameters dialog box and navigate to the Optimization > Signals and Parameters pane.
Select Inline parameters.
For more information about model referencing requirements, see Configuration Parameter Requirements in the Simulink documentation.
Save ex_timer_function_calls.
Reuse the timer function in multiple referenced blocks of a top model.
Create a new model named ex_modelref_utility_functions.
Add two Model blocks that reference ex_timer_function_calls.
Add four Outport blocks and connect them as follows:

Save ex_modelref_utility_functions.
Because the charts in each referenced block do not contain any exported graphical functions, you can reuse the timer function from the libTimerUtils library as many times as you want. For more information about model referencing, see Referencing a Model in the Simulink documentation.
![]() | Generating Reusable Code for Unit Testing | Rules for Using Atomic Subcharts in Stateflow Charts | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |