Data Store Basics
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.
When to Use a Data Store
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.
To share data between the instances of a reusable algorithm (for example, a subsystem in a custom library or a reusable referenced model), you can use a data store. For more information about data sharing for a reusable referenced model, see Share Data Among Referenced Model Instances.
Data Stores and Software Verification
Data stores can have significant effects on software verification, especially in the area of data coupling and control. Models and subsystems that use only input and output ports to pass data result in clean, well-specified, and easily verifiable interfaces in the generated code.
Data stores, like any type of global data, make verification more difficult. If your development process includes software verification, consider planning for the effect of data stores early in the design process.
For more information, see RTCA DO-331, “Model-Based Development and Verification Supplement to DO-178C and DO-278A,” Section MB.6.3.3.b.
Goto and From Blocks as a Signal Routing Alternative
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.
Local and Global Data Stores
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 (
A global data store is accessible from throughout the model hierarchy, including from referenced models. Define a global data store 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 References.
Data Store Diagnostics
About Data Store Diagnostics
Simulink® provides various run-time and compile-time diagnostics that you can use to help avoid problems with data stores. Diagnostics are available in the Model Configuration Parameters dialog box and the Data Store Memory block's parameters dialog box. The Simulink Model Advisor provides support by listing cases where data store errors are more likely because diagnostics are disabled.
Detecting Access Order Errors
Data Store Diagnostics and Models Referenced in Accelerator Mode
You can use data store run-time diagnostics to detect unintended sequences of data store reads and writes that occur during simulation. You can apply these diagnostics to all data stores, or allow each Data Store Memory block to set its own value. The diagnostics are:
These diagnostics appear in the Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory block pane, where each can have one of the following values:
Disable all— Disables this diagnostic for all data stores accessed by the model.
Enable all as warnings— Displays the diagnostic as a warning in the MATLAB Command Window.
Enable all as errors— Halts the simulation and displays the diagnostic in an error dialog box.
Use local settings— Allow each Data Store Memory block to set its own value for this diagnostic (default).
The same diagnostics also appear in each Data Store Memory block parameters
dialog box Diagnostics tab. You can set each
value specified by an individual block takes effect only if the corresponding
configuration parameter is
Use local settings. See
Model Configuration Parameters: Data Validity Diagnostics
and the Data Store Memory documentation
for more information.
The most conservative technique is to set all data store diagnostics to
Enable all as errors in Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory block. However, this setting is not best in all cases, because it can
flag intended behavior as erroneous. For example, the next figure shows a model
that uses block priorities to force the Data Store Read block to
execute before the Data Store Write block:
An error occurred during simulation because the data store
A is read from the Data Store Read block
before the Data Store Write block updates the store. If the
associated delay is intended, you can suppress the error by setting the global
parameter Detect read before write to
Use local settings, then setting that parameter
none in the Diagnostics
pane of the Data Store Memory block dialog box. If you use this
technique, set the parameter to
error in all other
Data Store Memory blocks aside from those that are to be
intentionally excluded from the diagnostic.
Data Store Diagnostics and Models Referenced in Accelerator Mode. For models referenced in Accelerator mode, Simulink ignores the following Configuration Parameters > Diagnostics > Data Validity > Data Store Memory block parameters if you set them to a value other than
Detect read before write (
Detect write after read (
Detect write after write (
You can use the Model Advisor to identify models referenced in Accelerator mode for which Simulink ignores the configuration parameters listed above.
In the Simulink Editor, in the Modeling tab, click Model Advisor.
Select By Task.
Run the Check diagnostic settings ignored during accelerated model reference simulation check.
Data Store Diagnostics and the MATLAB Function Block. Diagnostics might be more conservative for data store memory used by
MATLAB Function blocks. For example, if you pass arrays
of data store memory to MATLAB functions, optimizations such as
A = foo(A)
might result in MATLAB marking the entire contents of the array as read or written,
even though only some elements were accessed.
Detecting Multitasking Access Errors
Data integrity may be compromised if a data store is read from in one task and written to in another task. For example, suppose that:
A task is writing to a data store.
A second task interrupts the first task.
The second task reads from that data store.
If the first task had only partly updated the data store when the second task interrupted, the resulting data in the data store is inconsistent. For example, if the value is a vector, some of its elements may have been written in the current time step, while the rest were written in the previous step. If the value is a multi-word, it may be left in an inconsistent state that is not even partly correct.
Unless you are certain that task preemption cannot cause data integrity
problems, set the compile-time diagnostic Model
Configuration Parameters > Diagnostics > Data Validity > Data Store
Memory block > Multitask data store to
warning (the default) or
error. This diagnostic flags any case of a data
store that is read from and written to in different tasks. The next figure
illustrates a problem detected by setting Multitask data
Since the data store
A is written to in the fast task and
read from in the slow task, an error is reported, with suggested remedy. This
diagnostic is applicable even in the case that a data store read or write is
inside of a conditional subsystem. Simulink correctly identifies the task that the block is executing within,
and uses that task for the purpose of evaluating the diagnostic.
The next figure shows one solution to the problem shown above: place a rate transition block after the data store read, which previously accessed the data store at the slower rate.
With this change, the data store write can continue to occur at the faster rate. This may be important if that data store must be read at that faster rate elsewhere in the model.
The Multitask Data Store diagnostic also applies to data store reads and writes in referenced models. If two different child models execute a data store’s reads and writes in differing tasks, the error will be detected when Simulink compiles their common parent model.
Detecting Duplicate Name Errors
Data store errors can occur due to duplicate uses of a data store name within a model. For instance, data store shadowing occurs when two or more data store memories in different nested scopes have the same data store name. In this situation, the data store memory referenced by a data store read or write block at a low level may not be the intended store.
To prevent errors caused by duplicate data store names, set the compile-time
diagnostic Model Configuration Parameters > Diagnostics
> Data Validity > Data Store Memory block > Duplicate data store
error. By default, the value of the diagnostic is
none, suppressing duplicate name detection. The
next figure shows a problem detected by setting Duplicate
data store names to
The data store read at the bottom level of a subsystem hierarchy refers to a
data store named
A, and two Data Store Memory blocks in the
same model have that name, so an error is reported. This diagnostic guards
against assuming that the data store read refers to the Data Store Memory block
in the top level of the model. The read actually refers to the Data Store Memory
block at the intermediate level, which is closer in scope to the Data Store Read
Data Store Diagnostics in the Model Advisor
The Model Advisor provides several diagnostics that you can use with data stores. See these sections for information about Model Advisor diagnostics for data stores:
Check Data Store Memory blocks for multitasking, strong typing, and shadowing issues
Check data store block sample times for modeling errors
Check if read/write diagnostics are enabled for data store blocks
Specify Initial Value for Data Store
In general, to specify an initial value for a data store, you can use the same techniques that you use for other blocks. See Initialize Signals and Discrete States.
With most blocks, you can take advantage of scalar expansion to minimize the effort of specifying an initial value for a nonscalar signal. When you specify a scalar initial value, each element in the signal uses that scalar.
However, when you set the Dimensions parameter to
-1 in a Data Store Memory block (the default),
you cannot use scalar expansion. Instead, you must specify an initial value that has
the same dimensions as the stored signal. To take advantage of scalar expansion of
the initial value, set the Dimensions parameter to a specific
value such as
[1 2] or
[1 myDim] (for symbolic
Data Store Memory | Data Store Read | Data Store Write |