Import time and date text file

조회 수: 62 (최근 30일)
Lieke Numan
Lieke Numan 2019년 1월 31일
댓글: Lieke Numan 2019년 2월 12일
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