I can think of many different ways to approach this.
The most straight forward would be to pass in a format string to READTABLE however, that could be more difficult, depending on what else is in the file.
With the tab, or comma, or semicolon, this isn't a format that READTABLE will recognize as a single datetime value. It will try to break the data into two fields. A single "space" character between them might fix that, but in general I don't recommend modifying a file to force it to work with MATLAB's defaults.
There are two main cases:
1) You are the author of some code writing this data to a file and you want to read it back in later.
In that case, I'd modify the writing code to do two things:
wrap double-quotes around the data and use the default MATLAB datetime format--i.e. make it look like this:
"11-Feb-2019 13:08:45"
READTABLE will handle this very well.
2) You just have some files, and you want to read them in without changing the files.
Specificy the format to read the exact way you want.
readtable(filename,'Format','%{dd-M-yy,HH:mm:ss}D,'Delimiter','\n')
You don't want the file delimiter to be the same as the separating character in the format, or READTABLE will just split the data into two columns. Giving no delimiter avoids any confusion.
Of course, if there is more data--more variables in the file--this won't work.
3) Just my opinion
'Date' and 'Time' are both listed in the file as Variable Names, indicating these two pieces of data are separate. In that case: if I simply call READTABLE (I'm using 18b), then I get:
>> T = readtable('datetime2.txt')
T =
10×2 table
Date Time
___________ ________
'20-2-2017' 03:22:56
'20-2-2017' 03:22:56
'20-2-2017' 03:22:56
'20-2-2017' 03:22:56
'20-2-2017' 03:22:56
'20-2-2017' 04:10:57
'20-2-2017' 04:10:57
'20-2-2017' 04:10:57
'20-2-2017' 04:10:57
'20-2-2017' 05:03:03
The first variable is text, the second is duration (this might be text in older releases)
T.Date = datetime(T.Date,'InputFormat','dd-M-yy')
T.DateTime = T.Date + T.Time;
---
If you did get text for the second variable,
T.Time = timeOfDay(datetime(T.Time,'InputFormat','HH:mm:ss'))
will get to the duration value.
I hope this helps.
1 Comment
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/442498-import-time-and-date-text-file#comment_666366
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/442498-import-time-and-date-text-file#comment_666366
Sign in to comment.