Import time and date text file

57 次查看(过去 30 天)
Lieke Numan
Lieke Numan 2019-1-31
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 个评论
madhan ravi
madhan ravi 2019-1-31
T=readtable('sample.txt','DatetimeType','text');
% ^^^^^^^^^^----- your filename

请先登录,再进行评论。

回答(3 个)

Peter Perkins
Peter Perkins 2019-1-31
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 个评论
Lieke Numan
Lieke Numan 2019-2-1
编辑:Lieke Numan 2019-2-1
I am able to use either , or ; to separate the data (I added a file now). Could you help me with this file?
I have the data as well in excel like this:
20-2-17,03:22:56
Or like this:
20-2-17;03:22:56
Peter Perkins
Peter Perkins 2019-2-7
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
Ollie A 2019-1-31
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.
  1 个评论
Lieke Numan
Lieke Numan 2019-2-1
How can I turn this:
1×2 cell array
{'20-2-2017'} {'03:22:56'}
into a datetime vector?

请先登录,再进行评论。


Jeremy Hughes
Jeremy Hughes 2019-2-11
编辑:Jeremy Hughes 2019-2-11
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 个评论
Lieke Numan
Lieke Numan 2019-2-12
Thank you for the comprehensive response. The second option works perfect!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by