When you use model referencing to break a large system of models into smaller components and subcomponents, you can create data dictionaries to segregate the design data. Design data is the set of workspace variables that the models use to specify block parameters and signal characteristics. For basic information about data dictionaries, see What Is a Data Dictionary?.
To take this component-based approach to data management, create a shared dictionary that contains common data and a separate dictionary for each component that contains the data needed by that component.
This example shows how to partition design data into dictionaries. When you finish, each component in the system has a dictionary, and dictionary references allow the components to share data.
Navigate to the folder
(open).matlabroot
/help/toolbox/simulink/examples
Copy these files to a writable folder:
ProjectData_Contr.mat
ProjectData_ContrSub1.mat
ProjectData_ContrSub2.mat
ProjectData_ContrSubs.mat
ProjectData_Plant.mat
ProjectData_System.mat
ex_SystemModel
ex_PlantComp_Lvl1
ex_PlantComp_Lvl2
ex_ContrComp
ex_ContrComp_Sub1_Lvl1
ex_ContrComp_Sub1_Lvl2
ex_ContrComp_Sub2_Lvl1
ex_ContrComp_Sub2_Lvl2
Load the MAT-files to create design data in the base workspace.
Open the example model ex_SystemModel
. This
model is at the top of a reference hierarchy that includes the other
example models.
In the model, update the diagram. Each bus signal in the model
uses a Simulink.Bus
object as a
data type. The objects, SensorBus
and
CtrlBus
, are in the base workspace.
The referenced models ex_PlantComp_Lvl1
and
ex_ContrComp
use the bus objects for
root-level inputs and outputs, which means the plant and controller
components share the objects.
In the base workspace, double-click the Simulink.NumericType
object named
FloatType
. Signals, parameters, and other
data items in the controller component use this shared data
type.
In the Model Explorer Model Hierarchy pane, expand the node ex_SystemModel.
Click the Configurations
node. In the
Contents pane, the node Reference
to SimConfigSet
appears.
SimConfigSet
is a Simulink.ConfigSet
object in the base workspace. To
maintain configuration parameter uniformity for simulation, all of
the models in the hierarchy refer to
SimConfigSet
.
Right-click the node Controller (ex_ContrComp)
and select Open.
In the Model Explorer Model Hierarchy pane,
expand the new node ex_ContrComp
. Click the
Configurations
node.
In the Contents pane, the node
Reference to CodeGenConfigSet
appears.
CodeGenConfigSet
is a Simulink.ConfigSet
object in the base workspace. To
maintain configuration parameter uniformity for code generation, the
models in the controller component refer to
CodeGenConfigSet
. The models in the plant
component do not use CodeGenConfigSet
.
In the Model Hierarchy pane, select
Base Workspace. In the
Contents pane, right-click the variable
diff
and select Find Where
Used. In the Select a system
dialog box, select ex_SystemModel
and click
OK. If you see a message about updating
the diagram, click OK.
In the Contents pane, the variable
diff
is used by Constant
blocks in the models ex_ContrComp_Sub1_Lvl1
and
ex_ContrComp_Sub1_Lvl2
, which make up the
first controller subcomponent. Similarly, other models in the
hierarchy share the base workspace variables
coeff
, init
,
mu
, and rho
.
The table shows the models that share each variable in the base workspace.
Variable Name | Models Using the Variable | Scope of Sharing |
---|---|---|
CtrlBus | Top-level models in the plant and controller components | Shared globally by entire system |
SensorBus | Top-level models in the plant and controller components | Shared globally by entire system |
SimConfigSet | All models in the hierarchy | Shared globally by entire system |
rho | ex_PlantComp_Lvl2
,ex_ContrComp_Sub1_Lvl2 , and
ex_ContrComp_Sub2_Lvl2 | Shared globally by entire system |
mu | ex_PlantComp_Lvl1 and
ex_PlantComp_Lvl2 | Shared by models in the plant component |
FloatType | All models in the controller component | Shared by controller component and subcomponents |
CodeGenConfigSet | All models in the controller component | Shared by controller component and subcomponents |
init | ex_ContrComp_Sub1_Lvl2 and
ex_ContrComp_Sub2_Lvl1 | Shared by controller subcomponents |
diff | ex_ContrComp_Sub1_Lvl1 and
ex_ContrComp_Sub1_Lvl2 | Shared by models in the first controller subcomponent |
coeff | ex_ContrComp_Sub2_Lvl1 and
ex_ContrComp_Sub2_Lvl2 | Shared by models in the second controller subcomponent |
Suppose that separate teams of developers maintain the plant component and the controller components. You can use data dictionaries to store and scope the shared design data.
Create a shared global data dictionary that contains the data shared globally by the entire system.
In the Model Explorer, select File > New > Data Dictionary.
Set the new dictionary name to GlobalShare
and
click Save.
In the Model Hierarchy pane, right-click the
GlobalShare
node and select Show
Empty Sections.
In the Model Hierarchy pane, select
Base Workspace. In the
Contents pane, select the design data that
are shared globally by the entire system:
CtrlBus
, SensorBus
, and
rho
.
Right-click and select Copy.
In the Model Hierarchy pane, right-click the
Design Data
node under
GlobalShare
and select
Paste.
Similarly, copy SimConfigSet
from the
Base Workspace and copy to the
Configurations
node under
GlobalShare
.
Create a data dictionary for data shared by models in the Plant component. Add a reference from this dictionary to the shared global dictionary.
In the Model Explorer, select File > New > Data Dictionary.
Set the new dictionary name to Plant
and click
Save.
In the Model Hierarchy pane, select the node
Plant
. In the Dialog pane, under
Referenced Dictionaries, click
Add.
Double-click GlobalShare.sldd
.
In the Model Hierarchy pane, right-click the
node Plant
and select Save
Changes.
Link the Plant component to its component dictionary then migrate data shared by models in the Plant component from the base workspace to the dictionary.
Open the model ex_PlantComp_Lvl1
.
In the model, update the diagram.
If the Diagnostic View displays an error for multiple inconsistent
definitions of SimConfigSet
, select
Delete others next to the
GlobalShare
instance. This fix keeps the
definition in the GlobalShare
dictionary and
removes other definitions that can be seen by the model.
In the Modeling tab, under Design, click Link to Data Dictionary.
In the dialog box, click Browse.
Double-click Plant.sldd
.
In the Model Properties dialog box, click Apply. Click Change all models in response to the message about linking referenced models.
In the Model Properties dialog box, click Migrate data.
In the Migrate Data dialog box, select Include data from referenced models and then click Migrate.
(Optional) In the Model Properties dialog box, clear Enable model access to base workspace.
Remove the previous method for loading model data. In the
Model Properties dialog box, on the
Callbacks tab, clear the
PreLoadFcn
for the model.
Click OK.
Create a data dictionary that contains the data shared by models in the controller component. This dictionary can also reference the shared global dictionary.
In the Model Explorer, select File > New > Data Dictionary.
Set the new dictionary name to Controller
and
click Save.
In the Model Hierarchy pane, select the node
Controller
. In the Dialog pane, under
Referenced Dictionaries, click
Add.
Double-click GlobalShare.sldd
.
In the Model Hierarchy pane, right-click the
node Controller
and select Save
Changes.
Link the Controller component to its component dictionary then migrate data shared by models in the Controller component from the base workspace to the dictionary.
Open the model ex_ContrComp
.
If the Diagnostic View displays an error for multiple inconsistent
definitions of SimConfigSet
, select
Delete others next to the
GlobalShare
instance. This fix keeps the
definition in the GlobalShare
dictionary and
removes other definitions that can be seen by the model.
In the Modeling tab, under Design, click Link to Data Dictionary.
In the dialog box, click Browse.
Double-click Controller.sldd
.
In the Model Properties dialog box, click Apply. Click Change all models in response to the message about linking referenced models.
In the Model Properties dialog box, click Migrate data.
In the Migrate Data dialog box, select Include data from referenced models and then click Migrate.
(Optional) In the Model Properties dialog box, clear Enable model access to base workspace.
Remove the previous method for loading model data. In the
Model Properties dialog box, on the
Callbacks tab, clear the
PreLoadFcn
for the model.
Click OK.
Finally, link the top model to the global dictionary.
Open the model ex_SystemModel
.
In the Modeling tab, under Design, click Link to Data Dictionary.
In the dialog box, click Browse.
Double-click GlobalShare.sldd
.
In the Model Properties dialog box, click OK. Click Change this model only in response to the message about linking referenced models.
In the Model Explorer Model Hierarchy pane, select the
dictionary node Plant. In the Contents pane, to view the
contents of Plant.sldd
, click Show Current System
and Below
. The contents of the Design Data and
Configurations sections appear.
Similarly, view the contents of Controller.sldd
.
The DataSource column shows the variables and objects that each dictionary stores.
All of the globally shared variables, such as CtrlBus
and
SensorBus
, reside in GlobalShare.sldd
.
The variable init
, which both of the controller subcomponents
share, resides in Controller.sldd
.
If the development team assigned to the controller component must make changes
to the globally shared variables, they access the GlobalShare
dictionary file. Similarly, if the team must make changes to the variable
init
, they must access the Controller
dictionary file.
To view the entire dictionary and model hierarchy, perform a dependency analysis.
Open your saved model ex_SystemModel
.
On the Modeling tab, in the Design section, click Dependency Analyzer.
The system model, ex_SystemModel
, is linked to the
dictionary GlobalShare.sldd
. The plant component and the
controller component are each linked to a separate dictionary. To access the
shared data, the component dictionaries reference the dictionary
GlobalShare.sldd
. These dictionaries form a reference
hierarchy.
To learn how the models in a model reference hierarchy share data, use these techniques:
In an open model, on the Modeling tab, select Find > Find Ref Variables. The Model Explorer displays the variables that the model uses, as well as the variables that referenced models use. You can then right-click a variable and select Find Where Used to display all of the models that use the variable. For more information, see Edit and Manage Workspace Variables by Using Model Explorer.
At the command prompt, use the function Simulink.findVars
to
determine the variables a model uses. You can then use the function
intersect
to
determine the variables two models, components, or subcomponents
share.