Problems reading datetimes with readtable

3 visualizaciones (últimos 30 días)
dormant
dormant el 29 de Dic. de 2022
Comentada: dormant el 18 de En. de 2023
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 comentarios
the cyclist
the cyclist el 29 de Dic. de 2022
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 el 29 de Dic. de 2022
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.

Iniciar sesión para comentar.

Respuesta aceptada

Jeremy Hughes
Jeremy Hughes el 29 de Dic. de 2022
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

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by