Produce a Date vector starting with a desired date

Hi all,
I want to generate a date vector that starts at, say '01-Jan-2001 00:00:00' and skips by hour all the way up to '31-Dec-2031 23:00:00'.
I tried this:
Date = datevec('01-Jan-2011 00:00:00','dd-mmm-yyyy hh:mm:ss',
'31-12-2031 23:00:00');
But I don't think that's the way.
Any suggestions??
Any attempt to help is appreciated,
Pedro Cavaco

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 10 Mai 2011

4 Stimmen

a = datenum({'01-Jan-2011 00:00:00';'31-Dec-2031 23:00:00'});
Out = datevec(a(1):1/24:a(2));

7 Kommentare

The output is the same as a(2) :/
Instead of 1/24 you have to put nH.
The problem with my script is not the generation of the numerical dates, which IMHO are more secure with:
datenummx([o*dtv(1), o, o, (1:nH).', z,z])
The bottleneck is in datestr.
What andrei meant to write was this:
a = datenum({'01-Jan-2011 00:00:00';'31-Dec-2031 23:00:00'});
datevec(a(1):1/24:a(2));
I actually make the make mistake a lot too...
make the SAME mistake a lot too...
Ohhhh man it is working perfectly now, and quite fast as well.
Thanks a lot for the help guys
very large thank you Teja , again my typo ... corrected
My version had a small bug (nevertheless it did differentiate between 30,31 and lap years) but Andrei's version is really the best.
To convert to strings (takes time):
datestr(Out,0)
+1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by