Can't convert a cell array to a date using cellfun(@datenum or @datetime)
15 ビュー (過去 30 日間)
古いコメントを表示
Hi there,
I have a cell array that looks like:
'2012-03-12 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2010-10-24 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2012-02-06 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2012-05-22 00:00:00 UTC'
'2011-09-23 00:00:00 UTC'
'2011-06-21 00:00:00 UTC'
'2012-05-30 00:00:00 UTC'
'2012-05-31 00:00:00 UTC'
'2010-10-15 00:00:00 UTC'
And I am trying to use a function that will take the difference between the 16th of December and that date, in terms of days. I am trying this function but get an error which is
Error using datetime (line 602) Could not recognize the format of the date/time string '2012-03-12 00:00:00 UTC'. You can specify a format string using the 'InputFormat' parameter. If the string contains day, month, or time zone names in a language foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.
Here is my code snippet:
sincesignedup = days(datenum(2015,12,16,10,53,00)-datenum(cellfun(@datetime,finalnbs.signed_up_atEST)));
0 件のコメント
採用された回答
Guillaume
2015 年 12 月 17 日
You do not need to use cellfun to convert your cell array to datetime, but you do need to specify the format of your input string, and since you've got a timezone specified, which timezone you want the datetime in:
c = {'2012-03-12 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2010-10-24 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2012-02-06 00:00:00 UTC'
'2010-09-06 00:00:00 UTC'
'2012-05-22 00:00:00 UTC'
'2011-09-23 00:00:00 UTC'
'2011-06-21 00:00:00 UTC'
'2012-05-30 00:00:00 UTC'
'2012-05-31 00:00:00 UTC'
'2010-10-15 00:00:00 UTC'};
d = datetime(c, 'InputFormat', 'yyyy-MM-dd HH:mm:ss z', 'TimeZone', 'UTC')
The same using cellfun (which as stated is completely unnecessary):
d = cellfun(@(s) datetime(s, 'InputFormat', 'yyyy-MM-dd HH:mm:ss z', 'TimeZone', 'UTC'), c, 'UniformOutput', false);
d = vertcat(d{:})
1 件のコメント
Peter Perkins
2015 年 12 月 17 日
Guillaume is exactly right.
Another possibility, if ALL the strings contain 'UTC', would be to treat that as a literal, and create unzoned datetimes:
d = datetime(c, 'InputFormat', 'yyyy-MM-dd HH:mm:ss ''UTC''')
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!