|On this page…|
A data store is a repository to which you can write data, and from which you can read data, without having to connect an input or output signal directly to the data store. Data stores are accessible across model levels, so subsystems and referenced models can use data stores to share data without using I/O ports. You can define two types of data stores:
A local data store is accessible from anywhere in the model hierarchy that is at or below the level at which you define the data store, except from referenced models. You can define a local data store graphically in a model or by creating a model workspace signal object (Simulink.Signal).
A global data store is accessible from throughout the model hierarchy, including from referenced models. Define a global data stores only in the MATLAB® base workspace, using a signal object. The only type of data store that a referenced model can access is a global data store.
In general, locate a data store at the lowest level in the model that allows access to the data store by all the parts of the model that need that access. Some examples of local and global data stores appear in Data Store Examples.
For information about using referenced models, see Model Reference.
Data stores can be useful when multiple signals at different levels of a model need the same global values, and connecting all the signals explicitly would clutter the model unacceptably or take too long to be feasible. Data stores are analogous to global variables in programs, and have similar advantages and disadvantages, such as making verification more difficult. See Data Stores and Software Verification for more information.
In some cases, you may be able to use a simpler technique, Goto blocks and From blocks, to obtain results similar to those provided by data stores. The principal disadvantage of data Goto/From links is that they generally are not accessible across nonvirtual subsystem boundaries, while an appropriately configured data store can be accessed anywhere. See the Goto and From block reference pages for more information about Goto/From links.
To create a data store, you create a Data Store Memory block or a Simulink.Signal object. The block or signal object represents the data store and specifies its properties. Every data store must have a unique name.
Data stores implemented with Data Store Memory blocks:
Support data store initialization
Provide control of data store scope and options at specific levels in the model hierarchy
Require a block to represent the data store
Cannot be accessed within referenced models
Cannot be in a subsystem that a For Each Subsystem block represents.
Data stores implemented with Simulink.Signal objects:
Provide model-wide control of data store scope and options
Do not require a block to represent the data store
Can be accessed in referenced models, if the data store is global
Be careful not to equate local data stores with Data Store Memory blocks, and global data stores with Simulink.Signal objects. Either technique can define a local data store, and a signal object can define either a local or a global data store.
To write a signal to a data store, use a Data Store Write block, which inputs the value of a signal and writes that value to the data store.
To read a signal from a data store, use a Data Store Read block, which reads the value in the data store and outputs that value as a signal.
For Data Store Write and Data Store Read blocks, to identify the data store to be read from or written to, specify the data store name as a block parameter. See Access Data Stores with Simulink Blocks for more information.
You can log the values of a local or global data store data variable for all the steps in a simulation. See Log Data Stores.
Note: To use buses and arrays of buses with data stores, perform both the following procedure and Setting Up a Model to Use Data Stores with Buses and Arrays of Buses.
The following is a general workflow for configuring data stores. You can perform the tasks in a different order, or separately from the rest, depending on how you use data stores.
Where applicable, plan your use of data stores to minimize their effect on software verification. For more information, see Data Stores and Software Verification.
Create data stores using the techniques described in Data Stores with Data Store Memory Blocks or Data Stores with Signal Objects. For greater reliability, consider assigning rather than inheriting data store attributes, as described in Specifying Data Store Memory Block Attributes.
Add to the model Data Store Write and Data Store Read blocks to write to and read from the data stores, as described in Access Data Stores with Simulink Blocks.
Configure the model and the blocks that access each data store to avoid concurrency failures when reading and writing the data store, as described in Order Data Store Access.
Apply the techniques described in Data Store Diagnostics as needed to prevent data store errors, or to diagnose them if they occur during simulation.
If you intend to generate code for your model, see Data Stores in the Simulink® Coder™ documentation.
Benefits of using data stores with buses and arrays of buses include:
Simplifying the model layout by associating multiple signals with a single data store
Producing generated code that represents the data in the store data as structures that reflect the bus hierarchy
Writing to and reading from data stores without creating data copies, which results in more efficient data access
You cannot use a bus or array of buses that contains:
This procedure applies to local and global data stores, and to data stores defined with a Data Store Memory block or a Simulink.Signal object. Before performing the procedure, you must understand how to use data stores in a model, as described in Configure Data Stores.
To use buses and arrays of buses with data stores:
Use the Bus Editor to define a bus object whose properties match the bus data that you want to write to and read from a data store. For details, see Manage Bus Objects with the Bus Editor.
Add a data store (using a Data Store Memory block or a Simulink.Signal object) for storing the bus data.
Specify the bus object as the data type of the data store. For details, see Specify a Bus Object Data Type.
In the Model Configuration Parameters > Diagnostics > Connectivity pane, set the Mux blocks used to create bus diagnostic to error. For details, see Prevent Bus and Mux Mixtures.
If you use a MATLAB structure for the initial value of the data store, then in the Model Configuration Parameters > Diagnostics > Data Validity pane, set the Underspecified initialization detection diagnostic to error. For details, see Specify Initial Conditions for Bus Signals and Underspecified initialization detection.
(Optional) Select individual bus elements to write to or read from a data store. For details, see Accessing Specific Bus and Matrix Elements.