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

cell arrayscellfundatenumdatetimeMATLAB

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)));

Best Answer

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{:})