Convert char to date
27 次查看(过去 30 天)
显示 更早的评论
Basically, I have a table with dates that are currently char's and I want to convert them into datetime format. This is how the dates are now:
'2/4/22'
'2/5/22'
'2/6/22'
The problem is that these dates conflict with another dataset where the dates are like this:
2/4/22
2/5/22
2/6/22
I'm trying to make the dates appear the same for both tables and this was the command I ran:
Data.StudyDate_converted = cellfun(@(x) datetime(x), Data.StudyDate, 'UniformOutput', false);
This isn't doing the trick. The new column that the table creates in the table shows a 1x1 date time in each cell and when you click on it, the date reads as 04-Feb-0022
If someone knows a fix, please let me know. I attached the dataset that I want to convert from a char to datetime to this post in case its needed. The specific variable I want to convert from char to datetime is "StudyDate".
Thanks in advance
0 个评论
采纳的回答
Walter Roberson
2022-7-17
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1067985/Book5.xlsx';
Data = readtable(filename);
Data.StudyDate_converted = datetime(Data.StudyDate, 'Format', 'M/d/yy');
Data(1:3,:)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!