Problems reading datetimes with readtable

3 views (last 30 days)
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.
dormant on 29 Dec 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.

Sign in to comment.

Accepted Answer

Jeremy Hughes
Jeremy Hughes on 29 Dec 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

More Answers (0)


Find more on Dates and Time in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!