Problems reading datetimes with readtable

조회 수: 3 (최근 30일)
dormant
dormant 2022년 12월 29일
댓글: dormant 2023년 1월 18일
I have a bunch of text files with dates and times.
Some of them are like this (there is a space at the end of each line):
2022-12-18 00:59:24
2022-12-18 01:00:54
And some of them are like this (no space at the end):
2022-12-28 02:09:16 t
2022-12-28 03:59:56 t2
I want to end up with two arrays, whichever type of file I read. An array of datetimes and an array of strings, empty for the first type of file.
When I read the files using readtable, the first type of file returns an array of datetimes.
Reading the second type of file returns three arrays, an array of datetimes with only the dates, an array of durations and an array of the strings. When I try and add the durations to the datetimes, I get only the dates. Like this:
Is there a better approach? If not, why can't I add the duration to the datetime.
  댓글 수: 2
the cyclist
the cyclist 2022년 12월 29일
Can you upload some of the data instead of just describing it? You can use the paper clip icon in the INSERT section of the toolbar.
dormant
dormant 2022년 12월 29일
Thanks for the offer of help, but I managed to find a solution, using the 'Format' argument in datetime.
I'm new to datetime and am finding it a struggle at times.

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

채택된 답변

Jeremy Hughes
Jeremy Hughes 2022년 12월 29일
There is a slight issue with the file format in question. The second case looks to readtable like three columns if you choose space as a delimiter, so separating the "Datetime" from the "t" column isn't possible. If the file looked like:
2022-12-28 02:09:16, t
2022-12-28 03:59:56, t2
Then you wouldn't have the same issue as the datetime is separate from the other data.
With space as delimiter, I expect you to get a datetime, then a duration, then a string, and you can add durations to datetimes to get a new datetime array.
T.Date + T.Time

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by