datenum input string format

4 ビュー (過去 30 日間)
hasan
hasan 2013 年 7 月 18 日
I have dates created by another software which have a different formats than recognized by MATLAB. These days are like this:
'2012-06-01T03:15:00Z'
I want to feed these dates to datenum() and extract the date and time, but datenum doesn't support this format. Is there any way to define a custom time format in matlab? or any other suggestion please
Bests
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 18 日
Are T and Z always in this format?
hasan
hasan 2013 年 7 月 18 日
yes, the format is the same all the time, just the numbers change.

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

採用された回答

Jan
Jan 2013 年 7 月 19 日
編集済み: Jan 2013 年 7 月 19 日
You can try to use FEX: DateStr2Num. This is 50 to 100 times faster than the powerful datenum command.
Although the format does not exactly match the style 31: "yyyy-mm-dd HH:MM:SS", the space and the last character is ignored at all. So try:
Dnum = DateStr2Num('2012-06-01T03:15:00Z', 31);
A cell string can be used as input directly also.
An alternative dull but fast Matlab code:
S = '2012-06-01T03:15:00Z';
x = S - '0';
D = datenummx( ...
x(1)*1000 + x(2)*100 + x(3)*10 + x(4), ... % Year
x(6)*10 + x(7), ... % Month
x(9)*10 + x(10), ... % Day
x(12)*10 + x(13), x(15)*10 + x(16), x(18)*10 + x(19));
As long as the input is well formatted, this works nice. But Matlab's datenum is smarter and considers e.g. overflows etc.

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 18 日
編集済み: Azzi Abdelmalek 2013 年 7 月 18 日
d='2012-06-01T03:15:00Z'
a=regexp(d,'[^T Z]+','match')
b=datestr(datenum(horzcat(a{:}),'yyyy-mm-ddHH:MM:SS'))
% or
d='2012-06-01T03:15:00Z'
a=regexprep(d,'[T Z]+',' ')

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 18 日
d='2012-06-01T03:15:00Z'
out=datestr(datenum(d,'YYYY-mm-ddTHH:MM:SS'),'YYYY-mm-dd HH:MM:SS')

Andrei Bobrov
Andrei Bobrov 2013 年 7 月 18 日
q = '2012-06-01T03:15:00Z'
out = datenum(str2double(regexp(q,'\d*','match')));

カテゴリ

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