Main Content


Apply function to data contained in set of timeseries objects



    res = Simulink.SimulationData.forEachTimeseries(func,tsData) applies the function func to each timeseries object in tsData. You can use this function to postprocess data logged from a bus or array of buses.


    collapse all

    Open the model NonvirtualBusCreationModel. The model groups the output signals from a Chirp block and a Sine block into a bus that is nested in another bus. The second bus also contains the output signal from a Step block.

    Mark the bus TopBus for logging.

    Simulink.sdi.markSignalForStreaming("NonvirtualBusCreationModel/Bus Creator1",1,true);

    Simulate the model.

    out = sim("NonvirtualBusCreationModel");

    The Simulink.SimulationOutput object contains simulation metadata and all simulation data logged to the workspace.

    out = 
                    logsout: [1x1 Simulink.SimulationData.Dataset] 
                       tout: [53x1 double] 
         SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
               ErrorMessage: [0x0 char] 

    The variable logsout contains the data logged for the bus.

    logsout = out.logsout
    logsout = 
    Simulink.SimulationData.Dataset 'logsout' with 1 element
                             Name    BlockPath                               
                             ______  _______________________________________ 
        1  [1x1 Signal]      TopBus  NonvirtualBusCreationModel/Bus Creator1
      - Use braces { } to access, modify, or add elements using index.

    Access the signal that contains the data logged for the bus TopBus. The signal values are stored in the Values property for the Simulink.SimulationData.Signal object.

    topBusSig = getElement(logsout,1)
    topBusSig = 
      Package: Simulink.SimulationData
                  Name: 'TopBus'
        PropagatedName: ''
             BlockPath: [1x1 Simulink.SimulationData.BlockPath]
              PortType: 'outport'
             PortIndex: 1
                Values: [1x1 struct]
      Methods, Superclasses

    Access the structure that contains the values for the signals in the bus. The structure hierarchy and field names match the hierarchy and signal names for the bus. The data for each signal in the bus is stored as a timeseries object.

    topBusData = topBusSig.Values
    topBusData = struct with fields:
        NestedBus: [1x1 struct]
             Step: [1x1 timeseries]

    Use the Simulink.SimulationData.forEachTimeseries function to find the minimum value for each signal in the bus. The minimum values are returned as a structure with the same fields and hierarchy as the input data.

    topBusSigMins = Simulink.SimulationData.forEachTimeseries(@min,topBusData)
    topBusSigMins = struct with fields:
        NestedBus: [1x1 struct]
             Step: 0

    Check the minimum value for the output signal from the Chirp block.

    chirpMin = topBusSigMins.NestedBus.Chirp
    chirpMin = -0.9972

    Input Arguments

    collapse all

    Function to apply to data, specified as a function handle. For more information, see Create Function Handle.

    The function that you specify must take at least one input argument and return a scalar. You can specify a handle for a built-in function, a function you create, or an anonymous function.

    For example, you can use the built-in min function to compute the minimum value in each timeseries object.

    res = Simulink.SimulationData.forEachTimeseries(@min,tsData);

    When the function takes more than one input argument, only the argument for the timeseries object can change for each computation. The rest of the arguments must use the same values for each timeseries object.

    For example, to apply the resample function to each timeseries object, specify the function as an anonymous function. Here, the variable tsData represents the timeseries objects. The other arguments for the resample function are passed as parameter values. The Simulink.SimulationData.forEachTimeseries function passes the timeseres data you specify in the second input argument to the anonymous function as the value for tsData.

    res = Simulink.SimulationData.forEachTimeseries(@(tsData) resample(tsData,[2.5 3]),tsData);

    Data Types: function_handle

    Data to process, specified as one of these values:

    • Scalar timeseries object

    • Array of timeseries objects

    • Structure of timeseries objects

    • Array of structures of timeseries objects

    Output Arguments

    collapse all

    Results of processing data, returned as one of these values:

    • Scalar timeseries object

    • Array of timeseries objects

    • Structure of timeseries objects

    • Array of structures of timeseries objects

    Results are returned in the same form as the input data.

    Version History

    Introduced in R2016b