How can I create a table to work with, from this .txt?

조회 수: 1 (최근 30일)
Marcos M
Marcos M 2017년 11월 3일
답변: Peter Perkins 2017년 11월 21일
Hi! I need to create a table to do some research, however, I can't get what I want...My table should show 4 columns (1º Date 2ºTime 3º numeric value 4º numeric value), but when I used 'readtable' I only obtained one column with all the date on it... How can I fix it? (I attach my txt so you can see what I have to deal with)
clc
clear all
a=readtable('prueba.txt') %ALL THE DATA IN ONE COLUMN
T=readtable('prueba.txt','ReadVariableNames',false,'Format', ' %{dd/MM/yyyy}D %{HH:mm}D %f %f') %IT DOESNT WORK TOO
Thank you very much for your help

답변 (7개)

Chenchal
Chenchal 2017년 11월 3일
편집: per isakson 2017년 11월 3일
your readtable line is missing a '\n' maybe?
readtable('prueba.txt','ReadVariableNames',false,'Format', ' %{dd/MM/yyyy}D %{HH:mm}D %f %f\n')
  댓글 수: 1
Marcos M
Marcos M 2017년 11월 3일
편집: per isakson 2017년 11월 3일
No sorry, It keeps reporting the same error:
Unable to read the DATATIME data with the format 'dd/MM/yyyy'. If the data is not a time, use %q to get
string data.
I don't know what kind of mistake I might be doing with that .txt that I attached

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2017년 11월 3일
This works for me in R2017b:
a = readtable('prueba.txt', 'Format', '%{dd/MM/yyyy HH:mm}D %f %f', 'HeaderLines',1);
  댓글 수: 2
Marcos M
Marcos M 2017년 11월 4일
Thanks for your answer! I tried your script, however, the same kind of mistake keeps happening.
Error using readtable (line 135) Cannot interpret data in the file 'prueba.txt'. Found 1 variable names but 3 data columns. You may need to specify a different format string, delimiter, or number of header lines.
Star Strider
Star Strider 2017년 11월 4일
What version of MATLAB are your using?

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2017년 11월 3일
When I used the Import Tool to read in your data with Tab as the column delimiter, it was able to create three columns: the first is DTM a datetime, the second and third are numbers. You could import the data directly or create a script or function to automate the import process, if this is a representative of a large collection of files in the same format that you're going to want to import.
While you could probably split the datetime in the DTM variable into pieces for the date and the time, I think it would probably make sense to keep it together. This would be particularly useful if you want to import the data then convert it to a timetable with table2timetable. I suspect one of the operations you may want to perform is taking hourly or daily averages, which you can easily do on a timetable using the retime function.
  댓글 수: 1
Marcos M
Marcos M 2017년 11월 4일
Thanks! The Import Tool worked well, however, as you said, this file is a representative of a large collection of files in the same format, so I'm trying to write a script that I will import later.
I'm still trying to make it work

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2017년 11월 16일
You have a TAB delimited file, with three fields. The format you are telling readtable to use has FOUR variables in it. StarStrider is on the right track, but you need to specify the delimiter (or use detectimportoptions).

Chenchal
Chenchal 2017년 11월 16일
Star Strider's response works for me. I am using 2016b. here is a modification to read col names.
b = readtable('prueba.txt','Format', '%{dd/MM/yyyy HH:mm}D %f %f','ReadVariableNames', true);

Luay Hasiba
Luay Hasiba 2017년 11월 20일
hello! can you please attach your .m file because i need to something similar to what you asked about ? thanks :)

Peter Perkins
Peter Perkins 2017년 11월 21일
This is a TAB delimited file. In recent version of MATLAB, that is automatically detected, as is the type (datetime) of the first variable. You literally just need to call readtable with the file name.
>> t = readtable('prueba.txt');
>> head(t)
ans =
8×3 table
DTM g82mNWWSAVG g42mNWWSAVG
________________ ___________ ___________
01/01/2010 00:00 16.22 15.72
01/01/2010 00:10 15.29 14.95
01/01/2010 00:20 14.92 14.7
01/01/2010 00:30 15.84 15.49
01/01/2010 00:40 15.01 14.35
01/01/2010 00:50 15.02 14.5
01/01/2010 01:00 15.56 15.19
01/01/2010 01:10 15.04 14.48
In earlier versions, you will need to specify tab as the delimiter, and either use a format to read the datetime in directly, or read it in as text (the default in earlier versions) and convert to datetime once read in.

카테고리

Help CenterFile Exchange에서 Timetables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by