Problems reading datetimes with readtable

3 vues (au cours des 30 derniers jours)
dormant
dormant le 29 Déc 2022
Commenté : dormant le 18 Jan 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 commentaires
the cyclist
the cyclist le 29 Déc 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 le 29 Déc 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.

Connectez-vous pour commenter.

Réponse acceptée

Jeremy Hughes
Jeremy Hughes le 29 Déc 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

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by