Migrate Models to Use Simulink Data Dictionary
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?.
Migrate Single Model to Use Dictionary
To link a single standalone model to a single data dictionary.
Open a model that 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.
Note
Simulink does not import simulation data such as Timeseries objects into the data dictionary.
Migrate Model Reference Hierarchy to Use Dictionary
This example shows how to link a parent model and all its referenced models to a single data dictionary.
Open the
ex_SystemModel
model.TheopenExample('ex_SystemModel')
ex_SystemModel
model referencesPlant
andController
.Open the top model,
ex_SystemModel
.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.
Considerations Before Migrating to Data Dictionary
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.
Check for Data-Loading Callbacks
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 Dependency Analyzer to find data-loading callbacks. See Analyze Model Dependencies.
Check Scripts
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.Check Tunable Parameters for Code Generation
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 theseSimulink.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 toSimulink.Parameter
objects using thetunablevars2parameterobjects
function.
Valid Design Data Classes
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.ValueType
Simulink.VariantExpression
Simulink.data.dictionary.EnumTypeDefinition
embedded.fi
embedded.fimath
numlti
In addition, you can import, store, or create configuration objects of these classes in the Configurations section of a Simulink data dictionary:
Invalid Other Data Classes
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
Migration With From Workspace Blocks
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) or a structure with fields identical to a timeseries object, 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 theevalin
function. For more information, see From Workspace.
Data Dictionary Limitations
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
matlab.metadata.Class
objects andtimeseries
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
orNone
.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.
Evaluation of expressions that include Simulink variables or data types that are stored in a data dictionary might not be supported.
Continue to Use Shared Data in the Base Workspace
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.
Migrate Complicated Model Hierarchy with Shared Data
For examples, see Partition Data for Model Reference Hierarchy Using Data Dictionaries.