How to convert one column from string to datetime
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
Dyuman Joshi
le 18 Fév 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'))
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!