Main Content

get

Get element or collection of elements from Simulink.SimulationData.Dataset object

Description

example

element = get(dataset,index) returns the element in the Simulink.SimulationData.Dataset object, dataset, corresponding to the index. The getElement function uses the same syntax and behavior as the get function.

example

element = get(dataset,elName) returns the element or elements specified by elName.

  • If only one element has the name elName, the function returns that element.

  • If multiple elements have the name elName, the function returns a Simulink.SimulationData.Dataset object containing those elements.

example

element = get(dataset,{elName}) returns a Dataset object containing one or more elements with the name elName.

Examples

collapse all

Open the model sldemo_fuelsys, which models a fault-tolerant fuel control system. For more information about the model, see Model Fault-Tolerant Fuel Control System.

mdl = "sldemo_fuelsys";
open_system(mdl)

In the Configuration Parameters dialog box, in the Data Import/Export pane, note that:

  • The model is not configured to return simulation outputs as a single Simulink.SimulationOutput object.

  • The model uses the variable name sldemo_fuelsys_output for signal logging data.

Simulate the model.

sim(mdl);

Access the Dataset object sldemo_fuelsys_output, which contains the signal logging data.

sldemo_fuelsys_output
sldemo_fuelsys_output = 
Simulink.SimulationData.Dataset 'sldemo_fuelsys_output' with 10 elements

                         Name            BlockPath                                
                         ______________  ________________________________________ 
    1  [1x1 Signal]      ''              sldemo_fuelsys/EGO Fault Switch         
    2  [1x1 Signal]      air_fuel_ratio  sldemo_fuelsys/Engine Gas Dynamics      
    3  [1x1 Signal]      ''              sldemo_fuelsys/Engine Speed Fault Switch
    4  [1x1 Signal]      speed           sldemo_fuelsys/Engine_Speed_Selector    
    5  [1x1 Signal]      ''              sldemo_fuelsys/MAP Fault Switch         
    6  [1x1 Signal]      map             sldemo_fuelsys/MAP_Selector             
    7  [1x1 Signal]      ego             sldemo_fuelsys/O2_Voltage_Selector      
    8  [1x1 Signal]      ''              ...o_fuelsys/Throttle Angle Fault Switch
    9  [1x1 Signal]      throttle        sldemo_fuelsys/Throttle_Angle_Selector  
   10  [1x1 Signal]      fuel            sldemo_fuelsys/To Plant                 

  - Use braces { } to access, modify, or add elements using index.

To access Dataset object elements, you can use indexing with curly braces. For example, access the throttle element of the signal logging Dataset object using the index 9.

el9 = sldemo_fuelsys_output{9}
el9 = 
  Simulink.SimulationData.Signal
  Package: Simulink.SimulationData

  Properties:
              Name: 'throttle'
    PropagatedName: ''
         BlockPath: [1x1 Simulink.SimulationData.BlockPath]
          PortType: 'outport'
         PortIndex: 1
            Values: [1x1 timeseries]

The signal data is stored in the Values property of the Simulink.SimulationData.Signal object as a timeseries object. The time values are in the Time property of the timeseries object. The signal values are in the Data property.

el9.Values
  timeseries

  Common Properties:
            Name: 'throttle'
            Time: [204508x1 double]
        TimeInfo: tsdata.timemetadata
            Data: [204508x1 double]
        DataInfo: tsdata.datametadata
el9.Values.Data
ans = 204508×1

   10.0000
   10.0028
   10.0169
   10.0500
   10.1000
   10.1500
   10.2000
   10.2500
   10.2766
   10.2766
      ⋮

Simulate the model GetDatasetElements, which logs data generated by three source blocks using the Dataset format.

out = sim("GetDatasetElements");

By default, all logged data is returned in a single variable in the workspace as a Simulink.SimulationOutput object named out. Logged output data is grouped in Simulink.SimulationData.Dataset object with the default name yout. You can access the logged output data using dot notation.

out.yout
ans = 
Simulink.SimulationData.Dataset 'yout' with 3 elements

                         Name      BlockPath               
                         ________  _______________________ 
    1  [1x1 Signal]      SameName  GetDatasetElements/Out1
    2  [1x1 Signal]      SameName  GetDatasetElements/Out2
    3  [1x1 Signal]      DifName   GetDatasetElements/Out3

  - Use braces { } to access, modify, or add elements using index.

Use the get function to access the second element of the Dataset object using the index 2.

el = get(out.yout,2)
el = 
  Simulink.SimulationData.Signal
  Package: Simulink.SimulationData

  Properties:
              Name: 'SameName'
    PropagatedName: ''
         BlockPath: [1x1 Simulink.SimulationData.BlockPath]
          PortType: 'inport'
         PortIndex: 1
            Values: [1x1 timeseries]

The model GetDatasetElements logs data generated by three source blocks using the Dataset format. In the model, the signals coming from the Sine Wave block and the Constant block share the same name. The signal coming from the Pulse Generator block has a unique name.

open_system("GetDatasetElements")

Simulate the model.

out = sim("GetDatasetElements");

By default, all logged data is returned in a single variable in the workspace as a Simulink.SimulationOutput object named out. Access the Simulink.SimulationData.Dataset object that contains the logged output data using dot notation.

out.yout
ans = 
Simulink.SimulationData.Dataset 'yout' with 3 elements

                         Name      BlockPath               
                         ________  _______________________ 
    1  [1x1 Signal]      SameName  GetDatasetElements/Out1
    2  [1x1 Signal]      SameName  GetDatasetElements/Out2
    3  [1x1 Signal]      DifName   GetDatasetElements/Out3

  - Use braces { } to access, modify, or add elements using index.

Use the get function to return the element with the name DifName. Because the name DifName is unique, the function returns a Simulink.SimulationData.Signal object for that element.

el = get(out.yout,"DifName")
el = 
  Simulink.SimulationData.Signal
  Package: Simulink.SimulationData

  Properties:
              Name: 'DifName'
    PropagatedName: ''
         BlockPath: [1x1 Simulink.SimulationData.BlockPath]
          PortType: 'inport'
         PortIndex: 1
            Values: [1x1 timeseries]

You can also use the get function when an element is not unique. Because the name SameName is not unique, the function returns a Simulink.SimulationData.Dataset object containing the elements with the name SameName.

ds = get(out.yout,"SameName")
ds = 
Simulink.SimulationData.Dataset '' with 2 elements

                         Name      BlockPath               
                         ________  _______________________ 
    1  [1x1 Signal]      SameName  GetDatasetElements/Out1
    2  [1x1 Signal]      SameName  GetDatasetElements/Out2

  - Use braces { } to access, modify, or add elements using index.

The model GetDatasetElements logs data generated by three source blocks using the Dataset format. In the model, the signals coming from the Sine Wave block and the Constant block share the same name. The signal coming from the Pulse Generator block has a unique name.

open_system("GetDatasetElements")

Simulate the model.

out = sim("GetDatasetElements");

By default, all logged data is returned in a single variable in the workspace as a Simulink.SimulationOutput object named out. Access the Simulink.SimulationData.Dataset object that contains the logged output data using dot notation.

out.yout
ans = 
Simulink.SimulationData.Dataset 'yout' with 3 elements

                         Name      BlockPath               
                         ________  _______________________ 
    1  [1x1 Signal]      SameName  GetDatasetElements/Out1
    2  [1x1 Signal]      SameName  GetDatasetElements/Out2
    3  [1x1 Signal]      DifName   GetDatasetElements/Out3

  - Use braces { } to access, modify, or add elements using index.

Use the get function with a cell array containing a character vector to return a Dataset object. If the element name is unique, the get function returns a Dataset object containing one element. If the element name is not unique, the get function returns a Dataset object containing all elements with that name. For example, create a Dataset object containing the element named DifName.

ds = get(out.yout,{'DifName'})
ds = 
Simulink.SimulationData.Dataset '' with 1 element

                         Name     BlockPath               
                         _______  _______________________ 
    1  [1x1 Signal]      DifName  GetDatasetElements/Out3

  - Use braces { } to access, modify, or add elements using index.

The model AccessDatasetNestedBus contains nested arrays of buses. Two arrays of buses, Bus2 and Bus3, are marked for logging. topBus is logged using an Outport block. This example shows how to access Dataset elements within the bus hierarchy.

Open and simulate the model.

mdl = "AccessDatasetNestedBus";
open_system(mdl)
out = sim(mdl);

All logged data is returned in a single variable, out, as a Simulink.SimulationOutput object. Access the Dataset object that contains signal logging data, logsout, using dot notation.

ds = out.logsout
ds = 
Simulink.SimulationData.Dataset 'logsout' with 2 elements

                         Name  BlockPath                                
                         ____  ________________________________________ 
    1  [1x1 Signal]      Bus3  ...ssDatasetNestedBus/Matrix Concatenate
    2  [1x1 Signal]      Bus2  ...sDatasetNestedBus/Vector Concatenate1

  - Use braces { } to access, modify, or add elements using index.

Data for topBus is logged to the Dataset object yout.

ds2 = out.yout
ds2 = 
Simulink.SimulationData.Dataset 'yout' with 1 element

                         Name    BlockPath                   
                         ______  ___________________________ 
    1  [1x1 Signal]      topBus  AccessDatasetNestedBus/Out1

  - Use braces { } to access, modify, or add elements using index.

You can use the get function to access signal logging information for each element in the Dataset object. For example, use the get function to return the Simulink.SimulationData.Signal object for the array of buses named Bus2.

get(ds,"Bus2")
ans = 
  Simulink.SimulationData.Signal
  Package: Simulink.SimulationData

  Properties:
              Name: 'Bus2'
    PropagatedName: ''
         BlockPath: [1x1 Simulink.SimulationData.BlockPath]
          PortType: 'outport'
         PortIndex: 1
            Values: [2x1 struct]

The logged data values are stored in the Values property of the Signal object. For an array of buses, the data is logged as an array of MATLAB® structures.

get(ds,"Bus2").Values
ans=2×1 struct array with fields:
    a
    b

You can access a specific structure using the index of the structure within the array. For example, to access the structure that contains timeseries objects for the signals coming from the Constant blocks Constant6 and Constant7, use the index 2.

get(ds,"Bus2").Values(2)
ans = struct with fields:
    a: [1x1 timeseries]
    b: [1x1 timeseries]

Access a timeseries object within the structure using dot notation. For example, access the timeseries object for the signal coming from the Constant6 block.

get(ds,"Bus2").Values(2).a
  timeseries

  Common Properties:
            Name: 'a'
            Time: [51x1 double]
        TimeInfo: tsdata.timemetadata
            Data: [51x1 double]
        DataInfo: tsdata.datametadata

Signal values are stored in the Data property.

get(ds,"Bus2").Values(2).a.Data
ans = 51×1

     6
     6
     6
     6
     6
     6
     6
     6
     6
     6
      ⋮

Suppose that you did not want to mark Bus2 for logging. You can also get signal values for the signal coming from the Constant6 block using the Dataset object element Bus3.

get(ds,"Bus3").Values(2,2).a.Data
ans = 51×1

     6
     6
     6
     6
     6
     6
     6
     6
     6
     6
      ⋮

Similarly, you can also access signal values for the signal coming from the Constant6 block using the Dataset object element topBus.

get(ds2,"topBus").Values.Bus3(2,2).a.Data
ans = 51×1

     6
     6
     6
     6
     6
     6
     6
     6
     6
     6
      ⋮

Input Arguments

collapse all

Dataset object from which to get the element, specified as a Simulink.SimulationData.Dataset object.

Index of element to get, specified as a positive integer.

Name of Dataset object element to get, specified as:

  • A string reflecting the name of the Dataset object element.

  • A character array reflecting the name of the Dataset object element.

  • A cell array containing one character vector reflecting the name of the Dataset object element. To return a Dataset object that can contain one element, use this format. Consider this form when writing scripts.

Alternatives

Instead of using get or getElement, you can use curly braces to streamline the indexing syntax to access an element in a Dataset object. The index must be a positive integer that is not greater than the number of elements in the variable. For example, get the second element of the logsout dataset.

logsout{2}

You can also use the find function to get an element or collection of elements from a dataset.

Version History

Introduced in R2011a