Cell array to string to datetime conversion, parse error.

Hello,
I have a cell array of many data types. The first column stores dates and times of the format: '2016/04/16 10:06:05 PM'.
I have pulled the first column out,
here = T(:,1);
so I'm trying to obtain datetimes from them using,
f = 'yyyy/MM/dd h:mm:ss a';
d1 = datetime(here(1),'InputFormat',f,'Format',f);
but I get the following error:
Error using datetime (line 616)
Unable to parse ''2016/04/16 10:06:05 PM'' as a date/time using the format 'yyyy/MM/dd h:mm:ss a'
So it looks like I have extra quotes maybe? I'm not really sure. I tried changing the format f
'"yyyy/MM/dd h:mm:ss a"',
tried converting the cell to an array of strings,
here = string(here);
and an array of chars,
cell2mat(here);
but nothing has worked at all. I'm getting very frustrated.
I even tried converting it to a string and tried using the function erase() to get rid if potential extra quotes but nothing I've tried works. I need these to be datetimes to work with them with my other functions.
Any help would be wonderful, thank you.
*EDIT* I just read that datetime can handle cell arrays but that's not working either and I'm having the following returned where here is still a cell array:
Error using datetime (line 616)
Unable to parse date/time text using the format 'yyyy/MM/dd h:mm:ss a'.
I'm completely lost.

7 Commenti

That error message only makes sense of T is a string array, not a cell array. If T is a cell array then here = T(:,1) would be a cell array, and here(1) would be a scalar cell array, in which case you would have received the error
Input data must be a numeric array, a string array, a cell array containing character vectors, or a char matrix.
here is certainly a cell array. I've pulled it directly from T which was the large cell array and the data type of here remains cell. I've tried converting it. I've tried working with it without converting it too but I continue to get a parse error. I've tried with it as a cell, string, and char.
Erin Winkler
Erin Winkler il 19 Lug 2018
Modificato: Erin Winkler il 19 Lug 2018
@Walter I have just tried to convert T to a table with cell2table. Then I tried to the datetime command and got the error you speak of. But not before then.
Ah, during my testing I accidentally used a cell array containing a string object instead of a cell array containing character vectors.
Okay, yes, if you are getting that exact error message then the quotes are part of the character vectors. Try
datetime( regexprep(here, '''', ''), 'InputFormat', f, 'Format', f)
What format is here supposed to be in?
Never mind, a cell array. It looks like it worked! Thank you so very much sir!
If you wanted to put it in the answers, I'll gladly accept it.

Accedi per commentare.

 Risposta accettata

datetime( regexprep(here, '''', ''), 'InputFormat', f, 'Format', f)

1 Commento

That is with here being a cell array for anyone else confused in the future.
Thank you kindly!

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by