필터 지우기
필터 지우기

Can't convert a cell array to a date using cellfun(@datenum or @datetime)

조회 수: 10 (최근 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)));

채택된 답변

Guillaume
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
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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by