How to convert this string to a datetime?

조회 수: 6 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 14일
댓글: Stephen23 2022년 12월 14일
Hi! Unfortunately, MATLAB is not allowing me to convert this series of strings of date to a datetime series. Can anyone please help?
D = ['2003/06/24'
'2003/07/10'
'2003/07/10'
'2003/07/10'
'2003/07/18'
'2003/07/26'
'2003/08/03'
'2003/08/11'
'2003/08/19'
'2003/08/27'];
Any feedback will be greatly appreciated!
  댓글 수: 2
Steven Lord
Steven Lord 2022년 12월 14일
Can you show the command you're using to try to perform this conversion and describe what "not allowing" means in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 14일
편집: Ashfaq Ahmed 2022년 12월 14일
Thank you so much! It works. The reason why it wasn't working the previous time is -- all the strings I showed here were a column of a table. So, I first used the function table2array, and then used Stephen's code!

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 12월 14일
D = ['2003/06/24'
'2003/07/10'
'2003/07/10'
'2003/07/10'
'2003/07/18'
'2003/07/26'
'2003/08/03'
'2003/08/11'
'2003/08/19'
'2003/08/27'];
T = datetime(D, 'inputformat','u/M/d')
T = 10×1 datetime array
24-Jun-2003 10-Jul-2003 10-Jul-2003 10-Jul-2003 18-Jul-2003 26-Jul-2003 03-Aug-2003 11-Aug-2003 19-Aug-2003 27-Aug-2003
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 14일
Thank you so much! It works. The reason why it wasn't working the previous time is -- all the strings I showed here were a column of a table. So, I first used the function table2array, and then used your code!
Stephen23
Stephen23 2022년 12월 14일
"So, I first used the function table2array.."
It is simpler and more efficient to use basic indexing:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by