MATLAB: Converting decimal year into julian day or a [Y,M,D] vector?

time

Hi everyone, Just would like to know how to convert decimal year (e.g. 1988.903) into julian day or as a vector that tell me the month and the day? I get the idea that from the above example I have the year 1988 and that I should convert 0.903 into month etc… however I really do have an issue in that regards.
when doing: t=[1988.903, 1988.974, 1988.998]; x=datevec(t); or x=datestr(t,'dd-mm-yy'); –> it gives me for all 10 June 2005… which is not correct …
Any idea is welcome here, Many thanks, S.

Best Answer

That's not the correct usage of datevec and datestr.
The first thing you need to do is work out how many days are in that year (ie leap year or not). Then it's just a matter of adding the fractional days multiplied by the number of days in the year to the julian day at the beginning of the year.
Just use MatLab's date numbers for flexibility. The following function should convert your serial year to a MatLab date:
function [num] = ConvertSerialYearToDate( y )
year = floor(y);
partialYear = mod(y,1);
date0 = datenum(num2str(year),'yyyy');
date1 = datenum(num2str(year+1),'yyyy');
daysInYear = date1 - date0;
num = date0 + partialYear * daysInYear;
end
And then you can use datevec to split that into its components, or datestr to turn it into a string, etc.
-g-