Determine Where to Store Variables and Objects for Simulink Models
Simulink® data are objects and variables that you create in a workspace such as the base workspace or a data dictionary. Simulink data includes:
Numeric values for block parameters, such as
Simulink.Parameterobjects and MATLAB® variables
Signals, such as
Value types defined by
Model configuration sets
Simulation input and output data
You can store, partition, and share model data in a location that is appropriate for your design. The storage locations that you choose can depend on:
Your modeling goals
The model architecture (referenced models, subsystems, and other partitioning strategies) and component structure
The types of data that you use
Types of Data
Simulation data is the set of input data for a simulation and the set of output data that a simulation generates. For example, you can use variables to store input data that a simulation acquires through Inport blocks, and you can use Outport blocks, To Workspace blocks, and logged signals to export output data. For more information about loading, generating, and storing simulation data, see Comparison of Signal Loading Techniques and Save Simulation Data.
Design data is the set of variables and data objects that you use to specify block parameters and signal characteristics in a model and its generated code. For example, design data includes numeric MATLAB variables, value type objects, and parameter and signal data objects.
You can store design data in the base workspace, model workspaces, or the Design Data section of a data dictionary. To permanently store local design data with a model, use model workspaces. To share design data between models, use data dictionaries or the base workspace. Data dictionaries permanently store the data. By using data dictionaries, you can partition the data to ease readability and maintenance, and track changes. If you use the base workspace, to permanently store the data, you must save it in a MAT-file or create a MATLAB script that can regenerate the data.
Architectural data is composed of model interfaces, such as port interfaces, data types, and system wide constants and their platform properties that are shared across Simulink and architecture models.
You can store architectural data for your model in the Architectural Data section of a Simulink data dictionary. To add, edit, or remove this data you can use the Architectural Data Editor, see Graphically Manage Shared Interfaces, Data Types, and Constants for more information. You can also use the object API
Simulink.dictionary.ArchitecturalData, see Create Architectural Data Object and Use It to Configure Architectural Data for more information. You can move design data to the Architectural Data section using the
Configuration sets are sets of model configuration parameters. By default, configuration sets reside in the model file, so you do not need to store the sets separately from the model. However, you cannot share these configuration sets with other models.
To share configuration sets between models, you must create
Simulink.ConfigSetobjects. Each object represents a standalone configuration set. You can store these objects in the base workspace or in the Configurations section of a data dictionary. If you use data dictionaries, you can define the scope of each configuration set, compare different configuration sets, and track changes. A data dictionary inherently partitions configuration sets from other kinds of data.
Variant configuration objects store information about variant configurations, active and default variant settings, and definitions of the control variable associated with each configuration. You can use Variant Manager to create and edit variant configuration objects in data dictionaries or the base workspace. For more information, see Variant Manager for Simulink.
Types of Data Storage
Types of data storage include:
The MATLAB base workspace — Use the base workspace to store variables while you experiment with temporary models.
A model workspace — Use a model workspace to permanently store data that is local to a model.
A data dictionary — Use data dictionaries to permanently store global data, share data between models, and track changes made to data.
The table compares the characteristics of each storage location.
|Data Storage Type||File Storage||Model-Data Separation||Sync with Source File on Disk||Different Data for Different Models||Visibility|
|Model Workspace||Model file (||Optional||Yes (synchronization at model initialization)||Yes||Local|
|Data Dictionary||Simulink Data Dictionary file
In addition to these characteristics, data dictionaries also provide:
Change tracking for configuration sets
For information about the way that models interact with workspaces and workspace variables, see Symbol Resolution.
Temporary Data: Base Workspace
Use the base workspace to temporarily store data:
While you learn to use Simulink
When you need to quickly create variables while experimenting with modeling techniques
When you do not need to store the data permanently
To create variables in the base workspace, you can use the MATLAB command prompt or the Model Explorer. All open models can use the data that you create in the base workspace by default. To encapsulate your model, you can choose to disable access to the base workspace (see Continue to Use Shared Data in the Base Workspace).
If you use variables to specify numeric block parameters in the model, you can programmatically change the parameter values during simulation by using commands at the command prompt.
To permanently store base workspace data before you end a MATLAB session, save the data in a MAT-file or a script file. During a later session, you can load the data from the file. However, if you make changes to the data in the base workspace, you must save the data to the file again. Consider using a model workspace or data dictionary to permanently store data instead as your project scales up.
Local Data: Model Workspace
Each model has its own workspace for storing variable values. Variables in a model workspace are visible only in the scope of the model. The model workspace is initialized from a data source that can be a model file, a MATLAB file, or MATLAB code stored in the model file.
Use a model workspace to store data that you use only in the associated model. This data can include:
Model parameters, such as numeric variables that you use to specify block parameter values
Data objects, such as
Simulink.Parameterobjects, that you use to control parameter characteristics
You can improve model portability and establish data ownership by storing the data in the model workspace. In this case, the model file permanently stores the data.
In a model reference hierarchy, each model workspace acts as a unique namespace. Therefore, you can use the same variable name in multiple model workspaces. You can then assign different values for each model.
You can use the Model Explorer to manipulate model workspace data. Alternatively, you can use the command prompt or scripts in conjunction with the model workspace programmatic interface.
For more information about using model workspaces to store local data, see Model Workspaces.
Global and Shared Data: Data Dictionary
A data dictionary is a standalone file that permanently stores data. Similar to a header file, a data dictionary provides a location outside of the model that can store and share data definitions. Use data dictionaries instead of the base workspace to partition data, track changes, control access, and share data. If you link a model to a data dictionary, you can still use variables in the base workspace by configuring the base workspace access from either the model or the data dictionary. See Continue to Use Shared Data in the Base Workspace.
Use a data dictionary to store data that multiple models or system components share. This data can include:
Numeric variables that multiple models use to specify block parameter values.
Simulink.NumericTypeobjects that you use to specify data types in multiple models at the same time.
Data objects, including signal objects (such as
Simulink.Signal) that use a storage class other than
Auto. If you have a Simulink Coder™ license, these objects can represent signals and tunable parameters that appear as global variables in the generated code.
Simulink.Busobjects that you use to define interfaces of model components, such as referenced models.
Simulink.ConfigSetobjects that you use to maintain configuration parameter uniformity across multiple models.
Enumerated type definitions, which you store using
To use the data defined in a data dictionary, you must attach the data dictionary to your model. You can use this association to share data between a specific set of models.
When you use data dictionaries, you can partition the data by storing it in additional referenced dictionaries. Because data dictionaries in a hierarchy share the same namespace, each entry in a data dictionary hierarchy must use a unique name. Multiple definitions of the same symbol are allowed only if their class type and property values are the same. For more information, see Data Consistency in Model Hierarchy.
You can also use data dictionaries to associate data with a custom block library. When you define data objects, such as bus and enumeration types, in a data dictionary attached to a library, users of the library automatically gain access to the data types contained in the data dictionary when they drag a block from the library into their model. For more information, see Attach Data Dictionary to Custom Libraries.
Use the Model Explorer to edit dictionary data. Alternatively, use the command prompt or scripts in conjunction with the data dictionary programmatic interface.
For more information about data dictionaries, see What Is a Data Dictionary?
Custom External File Sources
If you use a
to simulate a model using different sets of variables, you can store these
variable sets in custom external file sources. To load data from an external
Write, test, and register a file adapter for your custom file format (for example,
.xlsxor XML). For more information, see Create External File Adapter for Loading Variables into Simulink.SimulationInput Object.
Use your custom file adapter to load variables for simulation by using
Considerations for Code Generation
If you intend to generate C code from a model (Simulink Coder), take these considerations into account.
If you apply a storage class other than
Autoto a signal object (such as
Simulink.Signal) to control the appearance of a signal or block state in the generated code, you cannot store the object in a model workspace. Store the object in the base workspace or a data dictionary. For more information about storage classes for signals and states, see C Data Code Interface Configuration for Model Interface Elements (Simulink Coder).
If you apply a storage class other than
Autoto a parameter object (such as
Simulink.Parameter), you can store the object in the base workspace, a model workspace, or a data dictionary. However, if you store the object in a model workspace, the code generator assumes that the containing model owns the parameter. For more information, see Code Generation Impact of Storage Location for Parameter Objects (Simulink Coder).
If you store an
AUTOSAR.Parameterobject in a model workspace, the code generator ignores the storage class that you specify for the object.
Managing Data Storage for Your Project
For small projects, the base workspace can be a convenient way to store design data. In particular, the base workspace works well for:
Quick parameter tuning
The base workspace provides ease of access and global visibility. However, data storage in the base workspace is temporary and to preserve that data between sessions you must save it to a script or MAT-file.
Other options for small projects include:
Model workspace — Improves standalone model portability by providing permanent design data storage within the model.
Data dictionary — Provides permanent design data storage outside the model.
You can use the model workspace and data dictionary simultaneously with the base workspace. Doing so provides a bridge for migrating your data to the model workspace and data dictionary alone as your project grows in complexity.
While the base workspace is convenient for small, single-user projects and rapid prototyping, this solution does not scale well for more complex projects that require encapsulation, distributed development, and scoped data. Complex projects involve a hierarchy of referenced models or subsystem references. Regardless of the type of models your hierarchy contains, you can distribute your design data based on the intended visibility of the data. In order to build scalable systems, distribute design data with the goal of keeping data visibility as local as possible.
The table shows the techniques you can use to store, partition, and manage design data and configuration sets.
|Modeling Scenario||Scenario Description||Storage Locations and Techniques|
Rapid prototyping and model experimentation
You want to create temporary data, such as variables to specify numeric block parameters, while you learn to use Simulink.
You want to experiment with modeling techniques.
You do not need to permanently store the data that you create.
Store data in the base workspace so you can quickly create and change the data.
You have a single model that does not depend on other systems for data. The model stands alone because it is not a piece of a larger system.
Store data in the model workspace to improve model portability. Use a data dictionary to store data that you cannot store in the model workspace.
Alternatively, store all of the model data in a data dictionary.
A system decomposed into multiple models
A large system is decomposed into multiple models in a hierarchy of referenced models, where data definitions are shared between parent and child models.
Store local model data in each model workspace.
Store system-wide definitions, such as common types, parameters, and constants, in a data dictionary that is referenced by all of the models (either directly or through another data dictionary).
Store data that the models share, such as bus objects and configuration sets, in a data dictionary. Link all of the models in the hierarchy to the data dictionary.
System of components
Reusable components, such as libraries or model references, which are designed for high reuse across teams or organizations.
Store local model data in model workspaces.
Parameterize the library block or model block by using mask parameters or model arguments.
For each reusable component, store the data types required for the interfaces of that component in a separate data dictionary and associate the model or library to that data dictionary.
Data Consistency in Model Hierarchy
Data dictionaries in a model hierarchy can contain multiple definitions of the same symbol. For a model to update, duplicate symbol definitions visible to a single model (coming from the base workspace, connected Simulink data dictionaries, or visible library dictionaries) must be consistent. Symbol definitions are consistent when their symbol names, class types, and property values are the same.
For example, model
M1 can see definitions of variable
K in two storage locations. In this case, the definitions of
K must be consistent.
If definitions for the same symbol are consistent, you can simulate the model in the normal, accelerator, and rapid accelerator modes, and generate code for the model. Duplicate symbols defined in the interface dictionary or in a data dictionary linked to an architecture model are not supported.
When a model can see multiple consistent definitions of a symbol, only one of
the definitions is displayed by the Model Data Editor and returned by
Simulink.findVars. It is this
definition that is used by the model during simulation.
As long as duplicate symbols visible to a single model are consistent, the current
model and the models below it in the model hierarchy can use symbols with the same
name but different values by setting the model parameter
off. In the
Model Properties dialog box, on the External Data tab, clear
the Enforce consistent data definitions across referenced
models check box. Alternatively, you can set the parameter to
off programmatically by using the
For example, models
M3 each have a
single definition of variable
K, but there are multiple
K in the model hierarchy. By turning data
consistency checking off in model
M3 can maintain different values
You might turn data consistency checking off to integrate components from different vendors that use similar names for variables. You might leave data consistency checking on to integrate components within an organization to ensure the components operate under the same conditions.
By default, the
EnforceDataConsistency parameter is set to
on. You cannot set
on for a model unless each of its referenced models and their
variants also have
EnforceDataConsistency set to
results in an error for:
Software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulations
Simulation of a protected model