Regarding Days to month

1 回表示 (過去 30 日間)
Uday
Uday 2011 年 7 月 31 日
I have some data from 2008-2011, which includes leap year 2008. my data sets has following values [year day hour ]
I have days in each year ( 1:365 or 1:366), i wanna to calculate the months.
Thank you

回答 (3 件)

Jan
Jan 2011 年 7 月 31 日
Standard:
datevec(datenum(2008, 1, 12))
>> [2008, 1, 12, 0, 0, 0]
Automatic months overflow:
datevec(datenum(2008, 1, 32))
>> [2008, 2, 1, 0, 0, 0]
Considers leap year:
datevec(datenum(2008, 1, 60))
>> [2008, 2, 29, 0, 0, 0]
datevec(datenum(2008, 1, 61))
>> [2008, 3, 1, 0, 0, 0]
  3 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 7 月 31 日
And how the automatic month overflow that Jan shows doesn't solve your problem? Have you tried it?
Jan
Jan 2011 年 7 月 31 日
@Pimple: I do not see the differnce. Let [Day] be the day number in the range 1:356 or 366. Then this gets the corresponding month: "D = datevec(datenum(Year, 1, Day, Hour, 0, 0)); Month=D(2)".
If this still does not help, please explain the wanted output exactly again.

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


the cyclist
the cyclist 2011 年 7 月 31 日
I would expect you to be able to accomplish what you want with some combination of the command datevec(), datenum(), and datestr(). If you can't figure it out, post the exact format of what you have, and what you want as output. Show us the code that you have tried yourself.

Walter Roberson
Walter Roberson 2011 年 7 月 31 日
T = datevec(datenum(YourYear, 1, YourDays, YourHours, 0, 0));
TheMonth = T(:,2)
But if you want to calculate fraction of a year, you do not need to calculate the month number:
YearFraction = datenum(YourYear, 1, YourDays, YourHours, 0, 0) - datenum(YourYear, 1, 1, 0, 0, 0);
  2 件のコメント
Uday
Uday 2011 年 8 月 1 日
I used fractYear=year+ ((day-1)+(hour/24))./(365+(eomday(year,2)==29));
it works fine.
Jan
Jan 2011 年 8 月 1 日
@Pimple: I can see just a weak connection to your statement "i wanna to calculate the months". Please save our time by asking more precisely.

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by