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.

Risposte (5)

Andreas Goser
Andreas Goser il 9 Feb 2011

0 voti

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 il 9 Feb 2011

0 voti

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 Commenti

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

Accedi per commentare.

Brett Shoelson
Brett Shoelson il 9 Feb 2011

0 voti

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 Commenti

Claire
Claire il 9 Feb 2011
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 il 9 Feb 2011
The answer doesn't change; you can specify the hour in the variable h.
Brett
Claire
Claire il 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

Accedi per commentare.

Brett Shoelson
Brett Shoelson il 10 Feb 2011

0 voti

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

1 Commento

Claire
Claire il 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.

Accedi per commentare.

Walter Roberson
Walter Roberson il 10 Feb 2011

0 voti

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

2 Commenti

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

Accedi per commentare.

Categorie

Richiesto:

il 9 Feb 2011

Community Treasure Hunt

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

Start Hunting!

Translated by