MATLAB: Converting a date string to day of year

day of year conversion

Hi there I have a simple question that I can't figure out an answer to. I have a column of dates in mm/dd/yyyy format and I need to convert the dates to a Day of Year (1 to 365/366) and then export a file that has the year in column A and the Day of Year in Column B. The data set starts on Jan 1st 1957 so obviously includes leap years. Any advice would be greatly appreciated. Thanks.

Best Answer

Assuming that your input data is a cell string:
DC = {'01/02/2010'; '02/02/2010'};
DV = datevec(DC); % [N x 6] array
DV = DV(:, 1:3); % [N x 3] array, no time
DV2 = DV;
DV2(:, 2:3) = 0; % [N x 3], day before 01.Jan
Result = cat(2, DV(:, 1), datenum(DV) - datenum(DV2));