Cell array to string to datetime conversion, parse error.

2 views (last 30 days)
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 Comments
Erin Winkler
Erin Winkler on 19 Jul 2018
Never mind, a cell array. It looks like it worked! Thank you so very much sir!
Erin Winkler
Erin Winkler on 19 Jul 2018
If you wanted to put it in the answers, I'll gladly accept it.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jul 2018
datetime( regexprep(here, '''', ''), 'InputFormat', f, 'Format', f)
  1 Comment
Erin Winkler
Erin Winkler on 19 Jul 2018
That is with here being a cell array for anyone else confused in the future.
Thank you kindly!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!