Time and Date

I've written some time and date code, but I'm sure there's a better way to do it. I had my data set up as dd:mm:yyyy hh:mm:ss with each one in a separate column with the rest of my input variables, so I did as follows:
load belmontemini;
Day = data(:,2);
Month = data(:,3);
Year = data (:,1);
Hour = data (:,4);
Minute = data (:,5);
Second = data (:,6);
.....
Time = [Day Month Year Hour Second Minute];
C= [Time Evap];
If anyone could point me in the right direction that would be great. I tried to work it out using datenum, but I just couldn't make it work. I'm very new to Matlab, and really confused sorry!!
Thank you.

2 件のコメント

Claire
Claire 2011 年 2 月 2 日
Sorry it's actually Day of the Year, so I need to convert that to Day and Month as well if possible? Thanks
Walter Roberson
Walter Roberson 2011 年 2 月 2 日
You need to clarify what output you expect.
Why do you have both month and day of the year?

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

回答 (2 件)

Oleg Komarov
Oleg Komarov 2011 年 2 月 2 日

0 投票

Quote: "I had my data set up as dd:mm:yyyy hh:mm:ss with each one in a separate column with the rest of my input variables, so I did as follows"
Not clear...did you have smt like:
  • Dates as strings:
data = {'01:01:2004 20:25:33';
'09:02:2005 20:25:42'}
  • Dates as datevec output (individual components):
data = [01 01 2004 20 25 33;
09 02 2005 20 25 42];
And what's the result you trying to achieve? Convert to serial date?
  • In the first case:
datenum(data, 'dd:mm:yyyy HH:MM:SS')
  • In the second case:
datenum(data(:,[3,2,1,4:6]))
Oleg
Walter Roberson
Walter Roberson 2011 年 2 月 2 日

0 投票

If you have the day of year but not the month, then
datenum([Year 0 DayOfYear Hour Minute Second])
will produce a serial date number with the Day Of Year information properly distributed into month and day, taking in to account leap years.
If you need a string output instead, use datestr() with the same input technique.
If you need the vector back again but want the DayOfYear adjusted to month + day, then you can datenum() and datevec() the result of that.

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2011 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by