MATLAB: Datevec with no leap year timeseries

calendardatevecleap yearnon-leap year

Hello,
I am having issues using the function datevec on a time series with a no-leap calendar. I have a 4000 year time series with a time series resembling:
[31, 59, 90, 120, 151, 181, 212…. 1460000]
I would like to create a matrix in the form [year, month, day] for the time series.
When I run the code,
datevalue = time + datenum(1,1,1)-2;
DateVec = datevec(datevalue);
everything runs fine, but the values are wrong for the year and month. I was wondering if there was a way to use the datevec function with a no-leap calendar.
Thank you for any comments or suggestions.

Best Answer

Here's a function I wrote for the task:
And in response to Jan's query as to why one would ever want to do this, I often need it when I want to compare model simulations (which often use a 365-day calendar) with observations. I'm going to guess that the OP has a similar need.
function dn = daynoleap2datenum(day, pivotyr)
%DAYNOLEAP2DATENUM Convert from days since, no leap to serial date number
%




% dn = daynoleap2datenum(day, pivotyr)
%
% A lot of model output is saved with a time scale of "days since
% YYYY-01-01 00:00:00", where every year has 365 day. This function
% converts those dates into a serial date number.
%
% Input variables:
%
% day: number of days since pivot year
%
% pivotyr: pivot year, i.e. year that day count begins (on Jan 1)
% Determine which years in range are leap years
nyr = max(day./365);
yrs = pivotyr:(pivotyr + nyr);
isleap = @(x) (mod(x,4)==0 & mod(x,100)~=0) | mod(x,400) == 0;
leapyrs = yrs(isleap(yrs));
% Calculate date numbers
dayofyear = rem(day, 365);
yr = floor(day/365) + pivotyr;
dn = datenum(pivotyr, 1, 1) + day;
for ileap = 1:length(leapyrs)
needsbump = yr > leapyrs(ileap) | (yr == leapyrs(ileap) & dayofyear > 59);
dn(needsbump) = dn(needsbump) + 1;
end