Main Content

setElement

Change Simulink.SimulationData.Dataset object element stored at specified index

Description

example

ds = setElement(ds,idx,el) replaces the element stored at the specified index idswith the element el. If idx is one greater than the number of elements in the Dataset object ds, the function adds the element el to the end of the Dataset object ds.

ds = setElement(ds,idx,el,name) gives the element the name specified by name. You can use name to identify an element that does not have a name. If the element already has a name, the function replaces it with the name specified in the name argument.

Examples

collapse all

Use the setElement function to:

  • Change the name of a Simulink.SimulationData.Dataset object element.

  • Add a new element to a Dataset object.

  • Replace a Dataset object element with another element.

Create a Dataset object.

ds = Simulink.SimulationData.Dataset;

Create four Signal objects.

el1 = Simulink.SimulationData.Signal;
el1.Name = 'A';
el2 = Simulink.SimulationData.Signal;
el2.Name = 'B';
el3 = Simulink.SimulationData.Signal;
el3.Name = 'C';
el4 = Simulink.SimulationData.Signal;
el4.Name = 'D';

Add the Signal objects named A and B to the Dataset object.

ds = addElement(ds,el1);
ds = addElement(ds,el2)
ds = 
Simulink.SimulationData.Dataset '' with 2 elements

                         Name  BlockPath 
                         ____  _________ 
    1  [1x1 Signal]      A     ''       
    2  [1x1 Signal]      B     ''       

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

You can use the setElement function to change the name of a Dataset object element. Change the name of the second signal from B to Bchanged.

ds = setElement(ds,2,el2,"Bchanged")
ds = 
Simulink.SimulationData.Dataset '' with 2 elements

                         Name      BlockPath 
                         ________  _________ 
    1  [1x1 Signal]      A         ''       
    2  [1x1 Signal]      Bchanged  ''       

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

Because the Dataset object ds contains two elements, you can use the setElement function with the index 3 to add the element named C to the Dataset object.

ds = setElement(ds,3,el3)
ds = 
Simulink.SimulationData.Dataset '' with 3 elements

                         Name      BlockPath 
                         ________  _________ 
    1  [1x1 Signal]      A         ''       
    2  [1x1 Signal]      Bchanged  ''       
    3  [1x1 Signal]      C         ''       

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

You can also use the setElement function to replace one element with another. Replace the element named A with the element named D.

ds = setElement(ds,1,el4)
ds = 
Simulink.SimulationData.Dataset '' with 3 elements

                         Name      BlockPath 
                         ________  _________ 
    1  [1x1 Signal]      D         ''       
    2  [1x1 Signal]      Bchanged  ''       
    3  [1x1 Signal]      C         ''       

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

Input Arguments

collapse all

Dataset object for which to set the element, specified as a Simulink.SimulationData.Dataset object.

Index of element, specified as positive integer.

  • To change an existing element, the index must be less than or equal to the number of elements in the Dataset object.

  • To add an element, the index must be one more than the number of elements in the Dataset object.

Element to replace existing element or to add to the data set, specified as a Simulink.SimulationData.Dataset object element.

When a Dataset object is created by logging simulation data, each element contains data for one logged signal, output, data store, or state. Each element is an object, and the type of the object depends on the data it contains.

When you create a Dataset object that groups simulation input data, each element contains data for a signal, bus, or array of buses. You can add data in any format supported by the loading method you use.

Type of InputData Formats

Scalar, vector, or multidimensional signal

Bus

  • Structure of timeseries, timetable, or matlab.io.datastore.SimulationDatastore objects that matches the hierarchy of the bus

  • Simulink.SimulationData.Signal

Array of buses

  • Array of structures

  • Simulink.SimulationData.Signal

Function-call signal

  • N-by-1 vector

  • Simulink.SimulationData.Signal

Element name, specified as a string or character vector.

Output Arguments

collapse all

Data set in which to change an element, specified as a character vector.

Alternative

You can use curly braces to streamline indexing syntax to change an element in a Dataset object instead of using setElement. The index must be a positive integer that is not greater than the number of elements in the variable. For example, change the name of second element of the logsout Dataset object.

logsout{2}.Name = 'secondSignal'

Version History

Introduced in R2011a