This function calculates the decimal day of the year of the input vector of dates. The dates must be in MATLAB serial date format.
For example, for datestr(now), MATLAB produces: "18-Jan-2008 14:33:27". The output of this function, called "date2doy" yields-
There are two outputs, the decimal date (can be a vector), and the fraction of the year completed by that date. From the example above, the fraction of the year is: "0.0481"
This function is very fast, and is ideal for embedding in larger applications that call date2doy many times.
I just submitted a doy2date.m. Thanks to both Anthony and James for the inspiration.
I am giving this a higher rating now because it is named properly and the new code can handle vectors. Thanks!
Not sure why FEX didn't have the date2doy. Should have been updated last January 18th, but something must have gone wrong there. I resubmitted the function.
The file that I downloaded is called date2julian when it should be called date2doy according to the file information.
I was looking for a function to convert from MATLAB serial date format to a day of year (1-365) and this one does that. It is just that the file name and documentation is misleading witht he mention of Julian dates.
Thanks, James, for your comments. You've helped me to make the code clearer and
I completely rewrote the function and resubmitted it.
I don't think it's as trivial as you suggest.
Also, Julian Day is very often in the Environmental Sciences used to refer to the day of the year. Terminology like that, essentially jargon, can have different definitions from field to field. But, to avoid confusion I've gone ahead and changed it to "doy" as you suggested.
I did not give this submission a good rating for the following reasons:
1) This routine basically returns the day of the year, not a Julian day. The term Julian day should be reserved for the astronomical definition of days since noon Universal Time (UT) Monday, January 1, 4713 BC. So the name of the function and the description should be something like datenum2doy.m, or the like. Descriptions like "... Julian days since the start of the year ... " are ambiguous and should be removed.
2) This routine takes a datenum type of variable as an input, but the variable name used by the author is date, and date is an existing MATLAB function name. This may be confusing to users. The name of the input date variable should be changed.
3) The functionality provided by this routine is fairly simple. For example, the following few MATLAB lines essentially do the same thing for the day-of-year calculation and are just as fast:
function doy = datenum2doy(dn)
dv = datevec(dn);
dv(:,2:end) = 0;
doy = dn - datenum(dv);
In summary, I gave this submission a low score because of the simple functionality provided and the misuse of the Julian day terminology. The author did comment his code, which is why I gave it a 2 instead of a 1. (you should always give people who comment their code *some* reward).
Submitted modified file to FEX as detailed in file description.
In response to James' review, I've heavily modified this function and taken all of his suggestions. Also, the calculation, while simple, is not as trivial as James mentions.
Create scripts with code, output, and formatted text in a single executable document.