Partition Data for Model Reference Hierarchy Using Data Dictionaries
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.
Open the Example Model and Load Design Data
Open the example model ex_SystemModel
. This model is at the top of a reference hierarchy that includes the other example models.
Load the MAT files contained in the working directory to create design data in the base workspace.
load ProjectData_Contr.mat load ProjectData_ContrSub1.mat load ProjectData_ContrSub2.mat load ProjectData_ContrSubs.mat load ProjectData_Plant.mat load ProjectData_System.mat
Create a Dictionary for Each 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.
Explore Example Model Hierarchy
In the model, update the diagram. Each bus signal in the model uses a
Simulink.Bus
object as a data type. The objects,SensorBus
andCtrlBus
, are in the base workspace.The referenced models
ex_PlantComp_Lvl1
andex_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 namedFloatType
. 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 nodeReference to SimConfigSet
appears.SimConfigSet
is aSimulink.ConfigSet
object in the base workspace. To maintain configuration parameter uniformity for simulation, all of the models in the hierarchy refer toSimConfigSet
.Right-click the node
Controller (ex_ContrComp)
and select Open.In the Model Explorer Model Hierarchy pane, expand the new node
ex_ContrComp
. Click theConfigurations
node.In the Contents pane, the node
Reference to CodeGenConfigSet
appears.CodeGenConfigSet
is aSimulink.ConfigSet
object in the base workspace. To maintain configuration parameter uniformity for code generation, the models in the controller component refer toCodeGenConfigSet
. The models in the plant component do not useCodeGenConfigSet
.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, selectex_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 modelsex_ContrComp_Sub1_Lvl1
andex_ContrComp_Sub1_Lvl2
, which make up the first controller subcomponent. Similarly, other models in the hierarchy share the base workspace variablescoeff
,init
,mu
, andrho
.
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 Shared Global Dictionary
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
, andrho
.Right-click and select Copy.
In the Model Hierarchy pane, right-click the
Design Data
node underGlobalShare
and select Paste.Similarly, copy
SimConfigSet
from the Base Workspace and copy to theConfigurations
node underGlobalShare
.
Create Dictionary for Plant Component
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 Plant Component to Dictionary and Migrate Data
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 Viewer displays an error for multiple inconsistent definitions of
SimConfigSet
, select Update others to match next to theGlobalShare
instance. This fix updates other definitions to be consistent with theGlobalShare
definition.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 Dictionary for Controller Component
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 Controller Component to Dictionary and Migrate Data
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 Viewer displays an error for multiple inconsistent definitions of
SimConfigSet
, select Update others to match next to theGlobalShare
instance.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.
Link System to Global Dictionary
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.
Inspect Data Storage
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.
Inspect Dictionary Hierarchy
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.
Strategies to Discover Shared Data
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 functionintersect
to determine the variables two models, components, or subcomponents share.