Day of year to month

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.

回答(5 个)

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 2011-2-9

0 个投票

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 个评论

Can you show us the form of your data?
This is not an answer to the question, please post comments or modify your question.
Please show us the code and the error messages.
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?

请先登录,再进行评论。

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 个评论

Thanks for that Brett, that helps, but what I really need is for it to have 24 hours for each date, as my data is hourly.
The answer doesn't change; you can specify the hour in the variable h.
Brett
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

请先登录,再进行评论。

How about this?
D = datevec(datenum(1992,1,1:1/24:365, 0,0,0));
Cheers,
Brett

1 个评论

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.

请先登录,再进行评论。

Walter Roberson
Walter Roberson 2011-2-10

0 个投票

I suspect you can skip all of this trouble and use datetick()

2 个评论

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?
http://www.mathworks.com/help/techdoc/ref/datetick.html
datetick('x','mmm HH')

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Dates and Time 的更多信息

提问:

2011-2-9

Community Treasure Hunt

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

Start Hunting!

Translated by