Import time and date text file
30 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a txt file which contains data like this:
20-2-17 03:22:56
20-2-17 04:10:57
20-2-17 04:10:57
20-2-17 04:10:57
20-2-17 04:10:57
20-2-17 05:03:03
I wanted to open the file in Matlab and make a datetime function with these data, but how can I load this data into matlab? Via Excel it didn't work as the date and time were in different columns and combining them caused the dates to change (from 01-03 to 03-01).
1 commentaire
madhan ravi
le 31 Jan 2019
T=readtable('sample.txt','DatetimeType','text');
% ^^^^^^^^^^----- your filename
Réponses (3)
Peter Perkins
le 31 Jan 2019
If that is LITERALLY what your text file looks like, then all you need to do is to give readtable an appropriate date format, something like dd-MM-yy hh:mm:ss. You can do that either with 'Format', or with detectimportoptions (the latter is preferable if you have a recent enough version).
If that's not what your file actually is, then you need to post an example of the file.
3 commentaires
Peter Perkins
le 7 Fév 2019
Read the file using readtable. Give it a format for the date portion, as described above. Depending on what version you have, the time protion may be read in as a duration or as text. If the latter, convert it using text2durationtext2duration. Then add the datetime and durations together, and throw away the duration.
Ollie A
le 31 Jan 2019
You could try using:
fid = fopen('filename.txt');
string = textscan(fid,'%s','delimiter','\n'); % Read in lines of string to variable as cell.
string = string{1};
dt = cell(length(string), 1);
for x = 1:length(string)
dt{x,1} = datetime(string{x},'InputFormat','dd-M-yy HH:mm:ss'); % Convert strings to datetime and store in a cell.
end
This stores all of the datetimes in a cell.
Jeremy Hughes
le 11 Fév 2019
Modifié(e) : Jeremy Hughes
le 11 Fév 2019
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.
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!