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

1 view (last 30 days)
Marcos M
Marcos M on 3 Nov 2017
Answered: Peter Perkins on 21 Nov 2017
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)
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

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
Marcos M
Marcos M on 3 Nov 2017
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

Sign in to comment.

Star Strider
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);

Sign in to comment.

Steven Lord
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.
  1 Comment
Marcos M
Marcos M on 4 Nov 2017
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

Sign in to comment.

Peter Perkins
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).

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);

Luay Hasiba
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 :)

Peter Perkins
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
________________ ___________ ___________
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.

Community Treasure Hunt

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

Start Hunting!

Translated by