About Data Stores

Local and Global Data Stores

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.

Customized Data Store Access Functions in Generated Code

Embedded Coder® provides a custom storage class that you can use to specify customized data store access functions in generated code. See Apply Custom Storage Classes and GetSet Custom Storage Class.

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. 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.

Create Data Stores

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.

Access Data Stores

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.

Data Store Logging

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.

Configure Data Stores

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.

  1. Where applicable, plan your use of data stores to minimize their effect on software verification. For more information, see Data Stores and Software Verification.

  2. 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.

  3. 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.

  4. 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.

  5. Apply the techniques described in Data Store Diagnostics as needed to prevent data store errors, or to diagnose them if they occur during simulation.

  6. If you intend to generate code for your model, see Data Stores in the Simulink® Coder™ documentation.

Data Stores with Buses and Arrays of Buses

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:

  • Variable-dimension signals

  • Frame-based signals

Setting Up a Model to Use Data Stores with Buses and Arrays of Buses

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:

  1. 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.

  2. Add a data store (using a Data Store Memory block or a Simulink.Signal object) for storing the bus data.

  3. Specify the bus object as the data type of the data store. For details, see Specify a Bus Object Data Type.

  4. 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.

  5. 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.

  6. (Optional) Select individual bus elements to write to or read from a data store. For details, see Accessing Specific Bus and Matrix Elements.

Was this topic helpful?