MATLAB: Converting Day of Year to month and day

arrayconversionday of yearmonths and datetime

Dear All
I have the following data:
epoch= '96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'
the firs 2 elements of the data is year '96' and rest is the day of year '318.74847837'. I got the following code to separate them.
year=cellfun(@(y) y(1:2),epoch,'uni',false);
day=cellfun(@(y) y(3:5),epoch,'uni',false);
And the following code to convert day of year to date and month.
doy=318;
dayspermonth = [0 31 28 31 30 31 30 31 31 30 31 30 31];
mm = ones(size(doy))*NaN;
dd = ones(size(doy))*NaN;
for im = 1:12
I = find(doy > sum(dayspermonth(1:im)) & doy <= sum(dayspermonth(1:im+1)));
mm(I) = ones(size(I)).*im;
dd(I) = doy(I) - ones(size(I))*sum(dayspermonth(1:im));
end
I would like to take the input of day into doy so that I have a single array of doy with all the results from day.
I know this code is not taking care of leap year. but if someone can help me with that I would be grateful.
Thanks!!

Best Answer

% Let y - our year
y = 1996;
doy = 318.25;
[yy mm dd HH MM] = datevec(datenum(y,1,doy));
variant of full solution
x= {'96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'} % initial array
a = str2double(x);
y = fix(a/1000);
[yy mm dd HH MM SS] = datevec(datenum(y,0,a - y*1000)); % corrected