How can I create a table to work with, from this .txt?
1 view (last 30 days)
Show older comments
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 Comments
Answers (7)
Chenchal
on 3 Nov 2017
Edited: per isakson
on 3 Nov 2017
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 Comment
Star Strider
on 3 Nov 2017
This works for me in R2017b:
a = readtable('prueba.txt', 'Format', '%{dd/MM/yyyy HH:mm}D %f %f', 'HeaderLines',1);
2 Comments
Steven Lord
on 3 Nov 2017
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
on 16 Nov 2017
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 Comments
Chenchal
on 16 Nov 2017
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 Comments
Luay Hasiba
on 20 Nov 2017
hello! can you please attach your .m file because i need to something similar to what you asked about ? thanks :)
0 Comments
Peter Perkins
on 21 Nov 2017
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 Comments
See Also
Categories
Find more on Timetables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!