converting csv timestamp to datetime on import

I have a CSV that has two columns, Date and Time. The general format of the text is colum 1, Date, is m/DD/YYYY, and column2, Time, is HH:mm:ss SSS. However, the output device outputs the text for time omitting the zero in cases where the digit is not bordered by other numbers in its value category. For example, one case reads out:
HH:m:ss S (11:1:30 1)
when it should read HH:mm:ss SSS (11:01:30 001) if it generated equal length strings.
I cannot change the output device to correct this to always include 'HH:mm:ss SSS' to perform a simple string input to use in the datetime(DateStrings,'InputFormat',infmt) method as the string changes length.

 채택된 답변

Stephen23
Stephen23 2022년 6월 28일
type test.csv
6/28/2022,11:1:30 1 06/28/2022,11:01:30 001
T = readtable('test.csv','Format','%{M/d/y}D%T%d','Delimiter',{',',' '})
T = 2×3 table
Var1 Var2 Var3 _________ ________ ____ 6/28/2022 11:01:30 1 6/28/2022 11:01:30 1
D = T.Var1 + T.Var2 + milliseconds(T.Var3);
D.Format = 'yyyy-MM-dd HH:mm:ss.SSS'
D = 2×1 datetime array
2022-06-28 11:01:30.001 2022-06-28 11:01:30.001

댓글 수: 2

Thank you for that answer. in the read table function, I see some guidance on how the 'Format' name value pair works, but it doesnt seem like a complete guide. do you have any links to resources to understand the full functionality of the 'Format' option?
The READTABLE documentation states for the FORMAT that "The conversion specifiers are the same as the specifiers accepted by the textscan function", so this would be the resource to read:

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 6월 28일

댓글:

2022년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by