Loop through a timeseries and calculate values over a 20min interval

19 views (last 30 days)
Siegmund Nuyts on 7 Oct 2022
Answered: Siegmund Nuyts on 13 Oct 2022
I have a timeseries with data every second for a few months and a function.
I'd like to use a subset of the timeseries as input in a function (every 20min - 1200 timesteps).
So [1:1200 1201:2400 2401:3600 ...] as input d.
How can I loop through the timeseries and get the value every 20min in an array [Time Pressure] and show the resulting value with a timstamp of 20min?
My initial idea was to use a for loop:
nt = ncread('pressure.nc', 'datetime');
Times = datetime(nt, 'convertFrom', 'posixtime', 'Format', 'yyyy-MM-dd HH:mm:ss');
pressure = ncread('pressure.nc', 'pressure');
T = timetable(Times, pressure);
d = T.pressure;
dt = 1 %timestep (1 second)
meth = 1
for i = 1:1200:length(d) %1200 timestepds = 20 min
[X] = myfunction(d, dt, meth)
end

Siegmund Nuyts on 13 Oct 2022
I found a solution using a different approach:
nt = ncread('pressure.nc', 'datetime');
Times = datetime(nt, 'convertFrom', 'posixtime', 'Format', 'yyyy-MM-dd HH:mm:ss');
pressure = ncread('pressure.nc', 'pressure');
T = timetable(Times, pressure);
dt = 1 %timestep (1 second)
meth = 1
interval = 1200;
timespan = length(T.pressure)-interval;
for jj = 1:interval:timespan
d = T.pressure(jj+(0:interval-1));
s = T.Times(jj);
[X] = myfunction(d, dt, meth)
XT = [XT; X]
Time = [Time; s]
end
TT = timetable(Time, XT);

Categories

Find more on Time Series Objects in Help Center and File Exchange

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by