Monthly average from time series with daily values

4 views (last 30 days)
I have 4 years of daily data in one file.
sla = ncread('data.nc','sla');
lon = ncread('data.nc','lon');
lat = ncread('data.nc','lat');
time = ncread('data.nc','time');
sla 181x101x1461 213668328 double
lat 101x1 404 single
lon 181x1 724 single
time 1461x1 5844 single
I want to calculate the monthly mean of the sla. The time vector comes out as
21916
21917
21918
21919
...
23376
%days since 1950-01-01
I want January to have 31 days, February to have 28/29 days, and so on. So I guess(?) I have to use
datestr(21916)
ans = 01-Jan-0060
I have tried to read through answers to similar questions, but that only made me more confused. Please explain on a basic level. Tnx!

Answers (3)

Jan
Jan on 20 Oct 2014
Edited: Jan on 20 Oct 2014
DV = datevec(double(time) + datenum('01-Jan-1950')); % EDITED
Now DV is a [1461 x 6] matrix. Now:
MonthIndex = DV(:, 1) * 12 + DV(:, 2)
gives an index for each month. accumarray will do the rest.
  2 Comments
K_r1
K_r1 on 20 Oct 2014
The datevec function only accepts double arrays. I get error bc my time vector is a single array.
Can I make it a double array?
Jan
Jan on 20 Oct 2014
Edited: Jan on 20 Oct 2014
See [EDITED] code. Are you surprised that the command "double()" converts to double format? Searching in Matlab's docs is a good idea, they are excellent.

Sign in to comment.


Chad Greene
Chad Greene on 5 Nov 2014
Edited: Chad Greene on 5 Nov 2014
t_daily = datenum(1950,1,1,double(time),0,0);
[sla_monthly,t_monthly] = downsample_ts(sla,t_daily);

Peter Perkins
Peter Perkins on 5 Nov 2014
K_r1, if you have access to MATLAB R2014b, there are some new data types that make this more straight-forward:
>> dt0 = datetime('1950-01-01')
dt0 =
01-Jan-1950
>> dt = dt0 + caldays([21916:250:23376]')
dt =
02-Jan-2010
09-Sep-2010
17-May-2011
22-Jan-2012
28-Sep-2012
05-Jun-2013
>> months = between(dt0,dt,'months')
months =
720mo
728mo
736mo
744mo
752mo
761mo

Community Treasure Hunt

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

Start Hunting!