A Simulink® data dictionary permanently stores model data including MATLAB® variables, data objects, and data types. For basic information about data dictionaries, see What Is a Data Dictionary?.
This example shows how to link a single standalone model to a single data dictionary.
Simulink does not import simulation data such as Timeseries objects into the data dictionary.
Open the f14
model, which loads design data
into the base workspace.
Save a copy of the model to your current folder. Open the copy.
In the Simulink Editor, on the Modeling tab, under Design, click Link to Data Dictionary.
In the Model Properties dialog box, click New to create a data dictionary.
Name the data dictionary, save it, and click Apply.
Click Migrate data.
Click Migrate in response to the message about copying referenced variables.
(Optional) Clear Enable model access to base workspace.
Click OK.
To open the dictionary, in the Simulink Editor, click the model data badge in the bottom left corner, then click the
External Data link. To inspect the contents
of the dictionary, in the Model Explorer Model
Hierarchy pane, under the External
Data node, expand the dictionary node.
This example shows how to link a parent model and all its referenced models to a single data dictionary.
Open the example model sldemo_mdlref_datamngt
, which
references the model
sldemo_mdlref_counter_datamngt
.
Save copies of the models to your current folder.
Open the top model, sldemo_mdlref_datamngt
.
In the Simulink Editor, on the Modeling tab, under Design, click Link to Data Dictionary.
In the Model Properties dialog box, click New to create a data dictionary.
Name the data dictionary, save it, and click Apply.
Click Change all models in response to the message about linking referenced models that do not already use a dictionary.
Click Migrate data.
Click Migrate in response to the message about copying referenced variables.
(Optional) Clear Enable model access to base workspace.
Click OK.
After you link a model to a data dictionary, you can choose to migrate data from the base workspace into the dictionary. If you choose to migrate data, take these considerations into account.
You can use model callbacks such as the PreLoadFcn
callback
to load design data from a file into the base workspace when a model is loaded.
For example, the following callback loads design data from the MAT file
myData.mat
.
load myData
After you migrate to a data dictionary, these callbacks will continue to load design data into the base workspace. Since the model then derives design data from the dictionary, manually remove or comment out these data-loading callbacks.
You can use the Simulink Manifest Tools to find data-loading callbacks. See Generate Model Dependencies Manifest.
A new model has access to the base workspace by default, but does not lose access when it is linked to a data dictionary. Scripts must be written with the assumption that the model can have access to the base workspace, the data dictionary, or both.
If you make explicit references to the base workspace by using the handle
base
in your scripts, consider changing these
references.
Consider this example. Here, the script searches the base workspace for
variable sensor
and sets the parameter
enable
depending on the value of
sensor.noiseEnable
.
if evalin('base','sensor.noiseEnable') enable = 'Enabled'; else enable = 'Disabled'; end
When you migrate to a data dictionary, replace these explicit references to
base
as
follows:
if Simulink.data.evalinGlobal(myExampleModel,... 'sensor.noiseEnable') enable = 'Enabled'; else enable = 'Disabled'; end
Simulink.data.evalinGlobal
function evaluates an expression in the global scope of the specified model.
Here, the global scope can be in a data dictionary or the base workspace, if the
model is not linked to a dictionary.If your model is linked to a data dictionary, and the model does not have access to the base workspace (see Continue to Use Shared Data in the Base Workspace), Simulink ignores storage class information specified in the Model Parameter Configuration dialog box.
If you use the Simulink
interface to migrate a model to use a data dictionary, and you choose to
migrate base workspace data, Simulink also migrates the storage class information of
the model. If your model contains storage class information for
variables in the base workspace, Simulink converts these variables into Simulink.Parameter
objects during
migration. Then, Simulink sets
the storage class of these Simulink.Parameter
objects using the storage class
information from the model.
If you migrate this model back to the base workspace, Simulink does not restore the storage class information in the model. To preserve the storage class for these variables, use the parameter objects from the data dictionary. You can also manually reset the storage class information in the model.
If you set the DataDictionary
property of a model
from the command line, you can convert tunable variables to Simulink.Parameter
objects using the
tunablevars2parameterobjects
function.
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.
The models in a model reference hierarchy typically share data. Data ownership, the number of shared variables, and the complexity of your sharing strategy can influence the way that you use dictionaries.
Duplicate data definitions can exist in a model reference hierarchy under these conditions:
Each model in the hierarchy can see only one definition.
Definitions must be the same across models in the hierarchy.
For more information, see Determine Where to Store Variables and Objects for Simulink Models.
You can import, store, or create MATLAB variables that use Simulink supported data types, such as
boolean
and int32
, and structures in
the Design Data section of a Simulink data dictionary. You can also use objects of these
classes and objects of most classes that subclass these classes:
Simulink.AliasType
Simulink.Bus
Simulink.NumericType
Simulink.Parameter
Simulink.LookupTable
Simulink.Breakpoint
Simulink.Signal
Simulink.Variant
Simulink.data.dictionary.EnumTypeDefinition
embedded.fi
embedded.fimath
numlti
In addition, you can import, store, or create configuration objects of the following classes in the Configurations section of a Simulink data dictionary.
You can import, store, or create data objects of many built-in and custom classes or data types in the Other Data section of a Simulink data dictionary, except for the following:
Arrays of objects created from built-in or custom classes
Custom classes that have a property with any of these names:
LastModified
LastModifiedBy
DataSource
Status
Variant
If a model contains a From Workspace block that refers to a variable in the base workspace, you can migrate the model to a data dictionary. However, the migration process takes different actions depending on the nature of the variable that the block refers to:
If the value of the variable is not a
timeseries
object, the migration process
imports the variable to the Design Data section of the data
dictionary. The block can still refer to the variable.
If the value of the variable is a timeseries
object (which a data dictionary cannot store), the migration process
does not import the variable. Then, when you try to update the
diagram or simulate the model, the From Workspace
block cannot find the variable and issues an error. In such a case,
you can configure the block to refer to the base workspace variable
by using the evalin
function. See Use with Data Dictionary.
Simulink cannot automatically migrate variables used only by inactive variant models into a data dictionary.
You cannot import certain kinds of design data such as
meta
class objects and
timeseries
objects into the Design Data section
of a data dictionary.
Simulink does not allow
implicit signal resolution for a model linked to a data dictionary. To
use a data dictionary, set the model configuration parameter
Signal resolution to Explicit
only
or None
.
If a model reference hierarchy is already linked to a data dictionary, you can protect a referenced model that is part of the hierarchy. However, if you migrate a model reference hierarchy that includes a protected model, simulation will fail.
In other words, migrate a model to use a data dictionary before protecting it.
You can continue to store shared data in the base workspace and store model-specific data in the data dictionary by:
Enabling access to the base workspace for the model.
Enabling access to the base workspace from a data dictionary.
To enable access to the base workspace for a model, in the Model Properties dialog box, on the External Data tab, select Enable model access to base workspace. For a new model, this check box is selected by default. If the model is not linked to a data dictionary, this option must be selected.
You can also allow access to the base workspace from a data dictionary. For an existing dictionary, in the Model Explorer, select Enable dictionary access to base workspace.
When you allow base workspace access from a data dictionary, these limitations and ramifications apply:
In general, you cannot interact with base workspace data through the dictionary.
When you inspect the contents of the dictionary in the Model Explorer, you cannot see base workspace data. To interact with base workspace data, in the Model Hierarchy pane, select the Base Workspace node.
With the programmatic interface of the data dictionary
(see Store Data in Dictionary Programmatically), to
interact with base workspace data, you can use only these
functions with a
Simulink.data.dictionary.Section
object:
assignin
exist
evalin
Consider using functions such as
Simulink.data.assigninGlobal
instead. See Transition to Using Data Dictionary.
Change-tracking features, such as the ability to view and revert changes to dictionary entries (see View and Revert Changes to Dictionary Entries), do not apply to base workspace data.
When you export data from a dictionary (see Import and Export Dictionary Data), Simulink ignores base workspace data.
Simulink treats the base workspace and the dictionary as a single namespace. However you can define two variables with the same name, one in the base workspace and one in the dictionary. In this case, the variables must be identical and the variable in the dictionary is used.
For examples, see Partition Data for Model Reference Hierarchy Using Data Dictionaries.
From Workspace | Reference Protected Models from Third Parties