Day of year to month

8 views (last 30 days)
Claire
Claire on 9 Feb 2011
I have a years worth of data in the form of 1-365 (in a column of my matrix), and I want to change it to the days in a month, so I can plot a graph that is quicker to read, rather than having to work out, what month day 100 etc.. belongs to.
I'm sure it's really easy to do but I'm completely out of my depth.

Answers (5)

Andreas Goser
Andreas Goser on 9 Feb 2011
I am not 100% sure, but maybe this example helps you
d2011=734504; % 734504 ist the first day of 2011 - example
v=d2011:d2011+100; % create a vector of 100 days
datestr(v) % see all the dates as strings - use for the figure
datevec(v) % can extract day of month

Claire
Claire on 9 Feb 2011
That does help thanks, but my problem now is that my data is hourly and so I need both time and data calculated to plot on my graph. I keep getting told that my vectors are different lengths, and I don't know how to rectify it.
  4 Comments
Jan
Jan on 9 Feb 2011
Please show us the code and the error messages.
Claire
Claire on 9 Feb 2011
Sorry Doug, I'll change it,
It's currently as:
Day = Belmonte(:,2);
Year = Belmonte (:,1);
Hour = Belmonte (:,3);
Minute = Belmonte (:,4);
Second = Belmonte (:,5);
And what I want is Year, Month, Day, Hour, Minute, Second, and then to be able to do a date in the format of Y, M, D, H, M, S, so that I can plot it against my evapotranspiration data which has a result for every hour. Does that make any sense?

Sign in to comment.


Brett Shoelson
Brett Shoelson on 9 Feb 2011
If you have 365 days, for instance, but no month information, you can simply increment the DAY position in datenum:
mydates = datenum(2011,1,1:365,h,m,s);
Or, if you want the y,m,d,h,m,s:
[y,m,d,h,m,s] = datevec(datenum(2011,1,1:365,h,m,s))
Cheers,
Brett
  3 Comments
Brett Shoelson
Brett Shoelson on 9 Feb 2011
The answer doesn't change; you can specify the hour in the variable h.
Brett
Claire
Claire on 10 Feb 2011
I've changed my code to D= datevec(datenum(1992,1,1:365, 1:24 ,00,00) but I'm still only getting 365 results whereas I need the 365 x 24.
Thanks

Sign in to comment.


Brett Shoelson
Brett Shoelson on 10 Feb 2011
How about this?
D = datevec(datenum(1992,1,1:1/24:365, 0,0,0));
Cheers,
Brett
  1 Comment
Claire
Claire on 10 Feb 2011
Thanks, that's improved the situation somewhat, although it seems to be still not quite right.
I've used D = datevec(datenum(1992,1,1:1/24:366)); but my Evapotranspiration data has 8760 rows, and the code I've used results in 8761 rows, and only one hour of results for 31/12
i.e. 1992, 12, 31, 0, 0, 0. I know 1992 is a leap year, but the data appears to only have 365 days, which I suspect is confusing the matter, although of course 8760/365 is 24, so that's just confused me more.
Thanks again for all your help.

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Feb 2011
I suspect you can skip all of this trouble and use datetick()
  2 Comments
Claire
Claire on 10 Feb 2011
Can you expand on that please? Don't I need to have an equal number of plots on my x and y axis to use datetick successfully?
Walter Roberson
Walter Roberson on 10 Feb 2011
http://www.mathworks.com/help/techdoc/ref/datetick.html
datetick('x','mmm HH')

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!