How to convert one column from string to datetime
13 views (last 30 days)
Show older comments
Hi everyone
I have a n by 3 matrix. The second column is date in string format. I need to convert the second column to datetime format "YYYY-MM-DD" and return the other 2 columns untouched. I thought this would work but I am getting an error:
ABC(:,2) = datetime(ABC(:,2));
The error is:
Error using datetime
Input data must be a numeric array, a string array, a cell array containing character vectors,
or a char matrix.
How can I fix my code?
Thank you
Accepted Answer
Dyuman Joshi
on 18 Feb 2024
%Random data for example
ABC = table(rand(5,1), {'2014-06-03';'2014-06-11';'2014-06-19';'2014-06-25';'2014-06-26'})
%Convert the 2nd column data to datetime
ABC = convertvars(ABC, 2, @(x) datetime(x, 'InputFormat', 'yyyy-MM-dd'))
More Answers (0)
See Also
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!