How can I create a table to work with, from this .txt?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (7개)
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')
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
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.
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).
댓글 수: 0
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);
댓글 수: 0
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 :)
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Timetables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!