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 件)

Andreas Goser
Andreas Goser 2011 年 2 月 9 日

0 投票

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 件のコメント

Brett Shoelson
Brett Shoelson 2011 年 2 月 9 日
Can you show us the form of your data?
Doug Hull
Doug Hull 2011 年 2 月 9 日
This is not an answer to the question, please post comments or modify your question.
Jan
Jan 2011 年 2 月 9 日
Please show us the code and the error messages.
Claire
Claire 2011 年 2 月 9 日
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?

サインインしてコメントする。

Brett Shoelson
Brett Shoelson 2011 年 2 月 9 日

0 投票

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 件のコメント

Claire
Claire 2011 年 2 月 9 日
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.
Brett Shoelson
Brett Shoelson 2011 年 2 月 9 日
The answer doesn't change; you can specify the hour in the variable h.
Brett
Claire
Claire 2011 年 2 月 10 日
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

サインインしてコメントする。

Brett Shoelson
Brett Shoelson 2011 年 2 月 10 日

0 投票

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

1 件のコメント

Claire
Claire 2011 年 2 月 10 日
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 件のコメント

Claire
Claire 2011 年 2 月 10 日
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 2011 年 2 月 10 日
http://www.mathworks.com/help/techdoc/ref/datetick.html
datetick('x','mmm HH')

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDates 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