Documentation Center

  • Trial Software
  • Product Updates

Access Data Stores with Simulink Blocks

Writing to a Data Store

To set the value of a data store at each time step:

  1. Create an instance of a Data Store Write block at the level of your model that computes the value.

  2. Set the Data Store Write block Data store name parameter to the name of the data store to which you want it to write data.

  3. Connect the output of the block that computes the value to the input of the Data Store Write block.

Reading from a Data Store

To get the value of a data store at each time step:

  1. Create an instance of a Data Store Read block at the level of your model that needs the value.

  2. Set the Data Store Read block Data store name parameter to the name of the data store from which you want it to read.

  3. Connect the output of the Data Store Read block to the input of the block that needs the data store value.

Accessing a Global Data Store

When connected to a global data store (one that is defined by a signal object in the MATLAB® workspace), a Data Store Read or Data Store Write block displays the word global above the data store name.

Accessing Specific Bus and Matrix Elements

Selecting Specific Bus or Matrix Elements

By default, a model writes and reads all bus and matrix elements to and from a data store.

To select specific bus or matrix elements to write to or read from a data store, use the Element Assignment pane of the Data Store Write block and the Element Selection pane of the Data Store Read block . Selecting specific bus or matrix elements offers the following benefits:

  • Reducing the number of blocks in the model. For example, you can eliminate a Data Store Read and Bus Selector block pair or a Data Store Write and Bus Assignment block pair for each specific bus element that you want to access).

  • Faster simulation of models with large buses and arrays of buses.

Writing Specific Elements to a Data Store

    Note:   The following procedure describes how to use the Data Store Write block interface to write specific elements to a data store. You can also perform this task at the command line, using the DataStoreElements parameter to specify elements. For details, see Specification using the command line.

To assign specific bus or matrix elements to write to a data store:

  1. Select the Data Store Write block and in the parameters dialog box, select the Element Assignment pane. For example, suppose you are using a bus with a data store named DSM:

  2. Expand all the elements in the Signals in the bus list.

  3. Specify the elements that you want to write to the data store. For example:

    • In the Signals in the bus list, click B. Then click Select>> to select the element B.

    • To write all the elements of A2 (in the A subbus), select A2[5x1]. Then click Select>>.

    • To write the second element of A2 in the C2 subbus, select the A2[5x1] element. In the Specify element(s) to assign text box, edit the text to say DSM.C.C2.A2(2,1).

    For more examples, see Specifying Elements to Assign or Select.

  4. (Optional) Reorder the assigned elements, which changes the order of the ports of the Data Store Write block.

    • To reorder an assigned element, in the Assigned element(s) list, select the element that you want to move, and click Up or Down.

    • To remove an assigned element, click Remove.

  5. To apply the assigned elements, click OK.

    The Data Store Write block has a port for each assigned element. The names of the selected elements that correspond to each port appear in the block icon. If you assign several signals, these additions may diminish the readability of the model. To improve readability, you can expand the size of the block or create multiple Data Store Write blocks.

Reading Specific Elements from a Data Store

Reading specific elements from a data store involves very similar steps as described in Writing Specific Elements to a Data Store. The Data Store Read block differs slightly from the Data Store Write block. A Data Store Read block has:

  • An Element Selection pane instead of an Element Assignment pane

  • A Selected element(s) list instead of an Assigned element(s) list

Specifying Elements to Assign or Select

Use MATLAB matrix element syntax to specify specific elements. For details about specifying matrices in MATLAB, see Creating and Concatenating Matrices.

    Note:   To select matrix elements, you cannot use dynamic indexing with the Element Assignment and Element Selection panes of Data Store Read and Bus Assignment block pairs or Data Store Write and Bus Selector block pairs. You can, however, use a MATLAB Function block for dynamic indexing.

Valid element specifications.  The following table shows examples of valid syntax for specifying elements to assign or select. These examples use the A2 subbus of the A bus, as shown in the bus hierarchy used in Writing Specific Elements to a Data Store.

Valid SyntaxDescription
DSM.A.A2(:,:)

Selects all elements in every dimension

DSM.A.A2([1,3,5],1)

Selects the first, third, and fifth elements

DSM.A.A2(2:5,1)

Selects the second through the fifth element

Invalid element specifications.  The following table shows examples of invalid syntax for specifying elements to assign or select. These examples use the A2 subbus of the A bus, as shown in the bus hierarchy used in Writing Specific Elements to a Data Store.

Invalid SyntaxReason the Syntax Is Invalid
DSM.A.A2(:)

You must specify a colon for each dimension. For the bus hierarchy used in these examples, you must use two colons.

DSM.A.A2(2:end,1)

You cannot use the end operator.

DSM.A.A2(idx,1)

You cannot use variables to specify indices. Consider using a MATLAB Function block.

DSM.A.A2(-1,1)

The dimension –1 is not within the valid dimension bounds.

Specification using the command line.  To set the elements to write to or read from, use the DataStoreElements parameter. Use a pound sign (#) to delimit multiple elements. For example, select the Data Store Write or Data Store Read block for which you want to specify elements and enter a command such as:

set_param(gcb, 'DataStoreElements', 'DSM.A#DSM.B#DSM.C(3,4)')

This specification results in the block now having three ports corresponding to the elements that you specified.

Was this topic helpful?