# MATLAB: Does anyone know of a function that will take timeseries data and find 12-hour averages throughout (day and night averages)

12 hour averagingbinningdaytime averagenight averagetimeseries

I have a 17 day timeseries and I would like to calculate 12-hour averages for the data so that I end up with daytime averages and nighttime averages. In this case "day" will be approx 06:00 to 18:00 (12 hours) and night is 18:00 to 06:00 (12 hours). I'm not sure if a function already exists to do this given timestamps and the data vector in question. If not would I want to loop through each 12 hour period? The night data might be problematic since it spans 2 dates. Anyway, I'm thrilled to hear any ideas/advice. I've attached my timestamps (MX) and example data (PH). Timestamps are in matlab time (serial date number from July 23, 2017 to August 9, 2017). Thank you!

``rt = TT.Properties.RowTimes;first_day = dateshift(rt(1), 'start, 'day');first_period = first_day + hours(6);if first_period > rt(1)    first_period = first_period - hours(12);elseif first_period + hours(12) < rt(1)    first_period = first_period + hours(12);endlast_day = dateshift(rt(end), 'start', 'day');last_period = last_day + hours(6);if last_period + hours(12) < rt(end)    last_period = last_day + hours(30);elseif last_period < rt(end)    last_period = last_period + hours(12);endlast_period_start = last_period - hours(12);periods = first_period : hours(12) : last_period_start;TT2 = retime(TT, periods 'mean');``