Call function on each timeseries object


dataResults = Simulink.SimulationData.forEachTimeseries(functionHandle,inputData)



dataResults = Simulink.SimulationData.forEachTimeseries(functionHandle,inputData) runs the specified function handle on all MATLAB® timeseries objects contained in inputData.


collapse all

This example shows how to use the forEachTimeseries function to run the min function on each timeseries object in the logged data for the COUNTERBUS signal.

Open the model and simulate it.


Access the signal logging data. For this model, that data is stored in the topOut variable.

Simulink.SimulationData.Dataset 'topOut' with 4 elements

                         Name          BlockPath                                
                         ____________  ________________________________________ 
    1  [1x1 Signal]      COUNTERBUS    sldemo_mdlref_bus/Concatenate           
    2  [1x1 Signal]      OUTERDATA     sldemo_mdlref_bus/CounterA              
    3  [1x1 Signal]      INCREMENTBUS  sldemo_mdlref_bus/IncrementBusCreator   
    4  [1x1 Signal]      INNERDATA     ...erA|sldemo_mdlref_counter_bus/COUNTER

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

Find the values for the COUNTERBUS element.

counterbusData = topOut{1}.Values
counterbusData = 

  2×1 struct array with fields:


Run the min function on the counterbus data.

ret = Simulink.SimulationData.forEachTimeseries(@min,counterbusData)
ret = 

2x1 struct array with fields:

Explore the returned data.

ans =

    data: 0
  limits: [1x1 struct]
ans =

  upper_saturation_limit: 40
  lower_saturation_limit: 0

Input Arguments

collapse all

Function to run on timeseries objects, specified as a function handle. For information about specifying function handles, see Pass Function to Another Function (MATLAB).

The function that you use with forEachTimeseries:

  • Can be either a built-in function or a user-specified function

  • Must return a scalar

If the function that you use with forEachTimeseries takes:

  • One argument, specify the function handle and the input data. For example:

    ret = Simulink.SimulationData.forEachTimeseries(@min,data);
  • More than one argument, specify the function handle as @(x) and then specify the function, using x as the first argument. For remaining arguments, specify values. For example, this command runs the resample function on MATLAB timeseries objects in data, for the time vector [2.5 3].

    ret = Simulink.SimulationData.forEachTimeseries(@(x)...
     (resample(x,[2.5 3]),data);

Data to run specified function on, specified as timeseries data.

Output Arguments

collapse all

Data resulting from running specified function, returned using the format and hierarchy of the input data.

Related Links

MATLAB timeseriesFunction Handles

Introduced in R2016b

Was this topic helpful?