MATLAB: How to find the day number of any year from a date

day number

I am very new in programming and therefore it is probably a novice's question. I have to write a script that takes a number of day(dd), month(mm) and year(yyyy)values arranged in three separate columns of a MATLAB variable and gives the day number of the year(e.g. for 03 March 2012, 31+29+3=63) as output.

Best Answer

If you are using R2014b or later, the datetime data type allows you to compute the day of year using the day function:
>> x = [2012 1 2; 2012 3 3; 2013 3 3]
x =
2012 1 2
2012 3 3
2013 3 3
>> d = datetime(x)
d =
02-Jan-2012
03-Mar-2012
03-Mar-2013
>> day(d,'dayofyear')
ans =
2
63
62