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);
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!