Convert ordered date to formal matlab date

2 ビュー (過去 30 日間)
Tunechi
Tunechi 2014 年 10 月 14 日
コメント済み: Sean de Wolski 2014 年 10 月 15 日
How to convert the following sequence of date to formal y/m/d or Matlab date format

採用された回答

José-Luis
José-Luis 2014 年 10 月 14 日
Note that you'd still need the year. But if you know that then:
your_date = datenum(your_year-1, 12, 31, 0, 0, 0) + ordinal_day;
Ordinal_day goes from 1 to 365.
  2 件のコメント
dpb
dpb 2014 年 10 月 14 日
What about leap years? OP shows multiple years possible in his listing w/o a starting year hint. Of course, he may be ignoring them...
José-Luis
José-Luis 2014 年 10 月 15 日
I assumed that if it was a leap year it'd say 366, but your point is valid.

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

その他の回答 (3 件)

Kelly Kearney
Kelly Kearney 2014 年 10 月 14 日
syear = 1900; % Year corresponding to first set of numbers
x = repmat(1:365, 1, 3)'; % Your data
yr = cumsum(x == 1) + (syear - 1);
dn = datenum(yr, ones(size(yr)), x); % datenumbers
If you want to format the date, you can use datestr:
>>datestr(dn(1))
ans =
01-Jan-1900

dpb
dpb 2014 年 10 月 14 日
編集済み: dpb 2014 年 10 月 15 日
Don't try to "convert", just create. Given the first year and length of the list, the date number vector would simply be
ystrt=2000; % or whatever
dn=datenum(ystrt,1,[1:length(dates)].',1);
datenum is smart enough to roll over the days by month and year including leap years.

Sean de Wolski
Sean de Wolski 2014 年 10 月 14 日
Or in R2014b you can do this with the new and improved datetime class:
datetime(2014,1,1:365,0,0,0)'
  4 件のコメント
dpb
dpb 2014 年 10 月 15 日
OK, so you were merely intending to emphasize the class as a class rather than some really enhanced functionality.
Wonder how the class implementation compares to "deadahead" array function performance-wise. Altho datenum is notoriously slow for all it's checking perhaps TMW has cleaned up a bunch of that and it's as fast or even faster...my machine here is limited and R2012b brings it to just barely tolerable performance so I've not tried the later revisions and I'm not at all eager to change UI.
Sean de Wolski
Sean de Wolski 2014 年 10 月 15 日
The class does have some enhanced functionality, especially when it comes to plotting and time zones.
I haven't measured the performance of it. Maybe Peter will chime in.

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

カテゴリ

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