MATLAB: Calculate daily, monthly, annual averages from hourly data

daily averagedatenum

I have hourly data, I want to be able to create daily, monthly and annual averages while taking into account the bisexile years.
These are hourly data from a netcdf file. So I have a temperature variable and a time variable
Data are available from January 1, 2003 to June 30, 2017
For temperature and time variables
Format:
classic
Dimensions:
time = 127080
Variables:
tas_sirta
Size: 127080x1
Dimensions: time
Datatype: single
Attributes:
units = '°C'
long_name = 'air_temperature'
standard_name = 'air_temperature'
name = 'tas'
dim = 'time'
Format:
classic
Dimensions:
time = 127080
Variables:
time
Size: 127080x1
Dimensions: time
Datatype: single
Attributes:
long_name = 'date_time'
units = 'hours since 1970-01-01'
actual_range = '289272.0, 416351.0'
standard_name = 'time'
name = 'time'
calendar = 'standard'
dim = 'time'
missing_value = NaN
I transform dates with datenum
timee = double(time)/24 + datenum('1970-01-01 00:00:00');
datestr(timee(1:7,1),'dd-mm-yyyy HH:MM:SS')
J'obtiens bien
ans =
01-01-2003 00:00:00
01-01-2003 01:00:00
01-01-2003 02:00:00
01-01-2003 03:00:00
01-01-2003 04:00:00
01-01-2003 05:00:00
01-01-2003 06:00:00
but I do not know then to make the daily, monthly and annual averages taking into account all the specificities of a year (February with 28-29 days, month with 30, 31 days …)
thank you

Best Answer

The simplest way to do what you want is to convert your data into a timetable:
hourlytemp = timetable(hours(time) + datetime(1970, 1, 1), tas_sirta, 'VariableNames', {'air_temperature'});
Then retime that timetable to monthly, daily, yearly or whatever period you want:
monthlyaverage = retime(hourlytemp, 'monthly', 'mean')
Done!