How to convert one column from string to datetime
13 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Dyuman Joshi
2024-2-18
%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'))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!