Main Content

Using Data Stores Across Multiple Models

This example shows how to use and log local data stores. It also shows how to define, use and log global data stores to share global data among referenced models.

Open the Example Models

Open the example models. The top modelsldemo_mdlref_dsmreferences modelssldemo_mdlref_dsm_botandsldemo_mdlref_dsm_bot2. Logging of data stores is turned on forsldemo_mdlref_dsmon the Data Import/Export pane of the Configuration Parameters dialog box.

Reference modelsldemo_mdlref_dsm_botamplifies the incoming signal by 5 if the signal is positive or by 2 if the signal is negative and writes the value of this signal to local data storeRefSignalVal. This local data store is configured to log all written values to the workspace after simulation. The logging parameters for this local data store are controlled by theLoggingtab of the block parameters dialog.

Reference modelsldemo_mdlref_dsm_bot2sets the value of a global boolean data store namedErrorCondto true if the value of its incoming signal is outside of the range (-0.8, 0.8). The top model also monitors the data storeErrorCondand switches between the outputs of the two referenced models, depending on the value of that data store.

Define a Global Data Store Using a Simulink® Signal Object

To specify to Simulink that the top model and both of the referenced models use the same memory for the data store namedErrorCondin each model, create an instance of aSimulink.Signalobject namedErrorCondin a workspace or data dictionary that is visible to each model. Since this object is visible to each model, its scope encompasses the individual models and implies that there is one global data store of that name that is shared among the models. Note that none of the three models contains a Data Store Memory block with a data store name ofErrorCond. If any model contained such a block, that block would define a data store local to the model, which would shadow the global data store.

Since the objectErrorConddefines a global data store, it must explicitly define theDataType,Complexity, andDimensionsproperties to non-inherited settings.SampleTimeandStorageClassmay be left as their inherited values, or explicitly set.

例子包括一个MAT-file命名sldemo_mdlref_dsm_data.matthat contains theSimulink.SignalobjectErrorCond. The preload function of the modelsldemo_mdlref_dsmloads this MAT-file causing the object to be loaded in the base workspace. You can view this object by selecting the base workspace in the Model Explorer. You can also create this object from the MATLAB® command prompt. The following commands define the object used in this example:

ErrorCond = Simulink.Signal; ErrorCond.Description = 'Use to signal that subsystem output is invalid'; ErrorCond.DataType = 'boolean'; ErrorCond.Complexity = 'real'; ErrorCond.Dimensions = 1; ErrorCond.SampleTime = 0.1; ErrorCond.LoggingInfo.DataLogging = true;

Logging a Global Data Store

To log all the values written to the global data store, set theDataLoggingproperty of theLoggingInfoproperty of theSimulink.Signalobject. TheLoggingInfoproperty also allows specification of other logging parameters such as decimation and maximum points.

Simulink.LoggingInfo (handle) DataLogging: 1 NameMode: 0 LoggingName: '' DecimateData: 0 Decimation: 2 LimitDataPoints: 0 MaxPoints: 5000

Simulate the Model

Now you can simulatesldemo_mdlref_dsmto see the output. The reference modelsldemo_mdlref_dsm_botwrites to the data storeErrorCondwhile the top modelsldemo_mdlref_dsmreads from the data store. The blue line represents the input tosldemo_mdlref_dsm_bot; the green line represents the output of the Switch block. Note that the output switches when the magnitude of the input signal falls outside of the range.

View Logged Results

Both the global data storeErrorCondand the local data storeRefSignalValare configured to log all written values after simulation. These logged results are stored in the base workspace in thedsmoutvariable.

dsmout = Simulink.SimulationData.Dataset 'dsmout' with 2 elements Name BlockPath ____________ ________________________________________ 1 [1x1 DataStoreMemory] ErrorCond '' 2 [1x1 DataStoreMemory] RefSignalVal ...dlref_dsm/A|sldemo_mdlref_dsm_bot/DSM - Use braces { } to access, modify, or add elements using index.

To access the data stored for the local data store, use thegetElementfunction to get the correct data store element based on the nameRefSignalVal.

>> dsmout.getElement('RefSignalVal')
Simulink.SimulationData.DataStoreMemory Package: Simulink.SimulationData Properties: Name: 'RefSignalVal' BlockPath: [1x1 Simulink.SimulationData.BlockPath] Scope: 'local' DSMWriterBlockPaths: [1x2 Simulink.SimulationData.BlockPath] DSMWriters: [101x1 uint32] Values: [1x1 timeseries]

Data is stored in atimeserieswithin theValuesfield.

>> plot(dsmout.getElement('RefSignalVal').Values);

To determine which block wrote to the data store at a given time, use theDSMWritersproperty. This array contains a list of indices into theDSMWriterBlockPathsarray. For example, the block path of the Data Store Write block that wrote the 5th value to the data store can be obtained as follows:

>> dsm = dsmout.getElement('RefSignalVal'); >> dsm.DSMWriterBlockPaths(dsm.DSMWriters(5))
Simulink.SimulationData.BlockPath Package: Simulink.SimulationData Block Path: 'sldemo_mdlref_dsm/A' 'sldemo_mdlref_dsm_bot/PositiveSS/DSW' Use the getBlock method to access block path character vectors from this object.

See Also

||

Related Topics