Code covered by the BSD License  

Highlights from
day of year

5.0

5.0 | 1 rating Rate this file 19 Downloads (last 30 days) File Size: 1.68 KB File ID: #27989

day of year

by Joshua Carmichael

 

23 Jun 2010 (Updated 25 Jun 2010)

Date vectors converted to day of the year.

| Watch this File

File Information
Description

Takes a date vector and returns the day of year, known incorrectly in the Geophysical community as the Julian calender day, i.e. 12/31/2005 is returned as day 365, day 06/22/2010 is returned as 173, etc... The function is vectorized. This function needs etime.m (R2009a and later).

USAGES
julday = datevec2doy(mydate)

INPUT
mydate: Either a 6xN or Nx6 array of date vectors, as output by
          functions like datevec.

OUTPUT
julday: An Nx1 array of julian days.

-----------------------------------------------------------------------
EXAMPLE
%Take the current day and add normally distributed random days to the
%date.

tadd = randn(1,12);
mydate = datevec(now)';
mydate = repmat(mydate,1,12);
mydate(2,:) = mydate(2,:) + tadd;
day = datevec2doy(mydate);

MATLAB release MATLAB 7.8 (R2009a)
Other requirements etime.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
24 Jun 2010 James Tursa

This submission has *nothing* to do with Julian Days. It is a misuse of the term. To see what Julian Date & Day mean you can read here:

http://en.wikipedia.org/wiki/Julian_day

Your function returns a day-of-year number. You should rename this submission datevec2doy or the like and remove the term Julian completely.

Also, something simple like this will not work:

d = datevec([now;now;now]);
datevec2julian(d)

because your function works with columns of vectors and not rows of vectors. Although your examples show that the input should be transposed, it might be nice if you handled both cases.

25 Jun 2010 Jan Simon

Help section: descriptive, examples, date and author defined, but no H1-line.
Function: inputs checked, works correctly. Useful and usable.

Another approach: Let Matlab's powerful DATENUM check and convert the input to accept a variety of formats, e.g. "13-Jan-2008", {"14-Feb-2008", "15-Mar-2010 18:21:13"}, [2010, 6, 6, 18, 21, 13], [now; now], DATEVEC([now, now]), etc:
  inNum = floor(datenum(in)); % check, convert, crop time
  inVec = datevec(inNum);
  inVec(:, 2:3) = 1; % January 1st
  out = 1 + inNum(:) - datenum(inVec);
This performs the same as ETIME, but omits the conversion from days to seconds and back to days.

26 Jun 2010 Oleg Komarov

This function could be much more useful if it was more flexible on the input, as suggested by Jan.
Also you can avoid using etime with another approach as well:
A H1 line is extremely important!

% Example input
ddate = '07/12/2003';
% Your fcn
datevec2doy(datevec(ddate))
%myFcn
yearday(ddate)

% It can be made as a one liner...
function dayOfYear = yearday(InDate)
nw = weeknum(InDate);
fiDay = weekday(InDate);
inDay = weekday([num2str(year(InDate)) '/01/01']);
% Calculate days using weeks
dayOfYear = 8-inDay + (nw-2)*7 + fiDay;
end

Oleg

26 Jun 2010 Oleg Komarov

Another faster and straightforward approach:

function dayOfYear = yearday(InDate)
InDate = datenum(InDate);
prevYear = datenum(year(InDate)-1, 12,31);
dayOfYear = InDate-prevYear;
end

Please login to add a comment or rating.
Updates
25 Jun 2010

name was inappropriate, and now it handles 6xN or Nx6 date arrays.

25 Jun 2010

changed title; previous title was misuse of Julian Days common definition

Tag Activity for this File
Tag Applied By Date/Time
julian day Joshua Carmichael 23 Jun 2010 10:29:43
calender Joshua Carmichael 23 Jun 2010 10:29:43
time Joshua Carmichael 23 Jun 2010 10:29:43
earth sciences Joshua Carmichael 23 Jun 2010 10:29:43
measurement Joshua Carmichael 23 Jun 2010 10:29:43
date conversion Joshua Carmichael 23 Jun 2010 10:29:43
geophysics Joshua Carmichael 23 Jun 2010 10:29:43
seismology Joshua Carmichael 25 Jun 2010 10:01:01
julian Joshua Carmichael 25 Jun 2010 13:51:08
calender kuoping 27 Jun 2010 23:50:33
julian day Imma 07 Nov 2010 08:34:20

Contact us at files@mathworks.com