Main Content

Data Store Basics

Adata storeis 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, seeShare Data Among Referenced Model Instances.

数据存储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.

GotoandFromBlocks as a Signal Routing Alternative

In some cases, you may be able to use a simpler technique,Gotoblocks andFromblocks, 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 theGotoandFromblock reference pages for more information about Goto/From links.

Local and Global Data Stores

You can define two types of data stores:

  • Alocal data storeis 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).

  • Aglobal data store整个模型层次结构来进行访问,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 inData Store Examples.

For information about using referenced models, seeModel References.

Data Store Diagnostics

About Data Store Diagnostics

金宝app®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

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 theModel Configuration Parameters > Diagnostics > Data Validity > Data Store Memory blockpane, 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 boxDiagnosticstab. You can set each diagnostic tonone,warning, orerror. The value specified by an individual block takes effect only if the corresponding configuration parameter isUse local settings. SeeModel Configuration Parameters: Data Validity Diagnosticsand theData Store Memorydocumentation for more information.

The most conservative technique is to set all data store diagnostics toEnable all as errorsinModel 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 theData Store Readblock to execute before theData Store Writeblock:

An error occurred during simulation because the data storeAis read from theData Store Readblock before theData Store Write块更新商店。如果associated delay is intended, you can suppress the error by setting the global parameterDetect read before writetoUse local settings, then setting that parameter tononein theDiagnosticspane of theData Store Memoryblock dialog box. If you use this technique, set the parameter toerrorin all otherData Store Memoryblocks 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 followingConfiguration Parameters>Diagnostics>Data Validity>Data Store Memory blockparameters if you set them to a value other thanDisable all.

  • Detect read before write(ReadBeforeWriteMsg)

  • Detect write after read(WriteAfterReadMsg)

  • Detect write after write(WriteAfterWriteMsg)

You can use the Model Advisor to identify models referenced in Accelerator mode for which Simulink ignores the configuration parameters listed above.

  1. In the Simulink Editor, in the建模tab, clickModel Advisor.

  2. SelectBy Task.

  3. Run theCheck diagnostic settings ignored during accelerated model reference simulationcheck.

Data Store Diagnostics and the MATLAB Function Block.Diagnostics might be more conservative for data store memory used byMATLAB Functionblocks. For example, if you pass arrays of data store memory to MATLAB functions, optimizations such asA = 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:

  1. A task is writing to a data store.

  2. A second task interrupts the first task.

  3. 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 diagnosticModel Configuration Parameters > Diagnostics > Data Validity > Data Store Memory block > Multitask data storetowarning(the default) orerror. 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 settingMultitask data storetoerror:

Since the data storeAis 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.

TheMultitask Data Storediagnostic 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 diagnosticModel Configuration Parameters > Diagnostics > Data Validity > Data Store Memory block > Duplicate data store namestowarningorerror. By default, the value of the diagnostic isnone, suppressing duplicate name detection. The next figure shows a problem detected by settingDuplicate data store namestoerror:

The data store read at the bottom level of a subsystem hierarchy refers to a data store namedA, 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 block.

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. SeeInitialize 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 theDimensionsparameter to-1in aData Store Memoryblock (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 theDimensionsparameter to a specific value such as[1 2]or[1 myDim](for symbolic dimensions).

See Also

|||

Related Topics