Read and sort data in a text file

8 visualizaciones (últimos 30 días)
Djamil Boulahbal
Djamil Boulahbal el 16 de Sept. de 2020
Editada: Djamil Boulahbal el 17 de Sept. de 2020
Greetings,
I have a text/ascii file that looks like below. The first line contains the variables names, and all lines beyond the first contain a mix of numerical and string values. The data is not stored in any specifc/ascending/descending order. What I'd like to do is load all the data into arrays (or structures?) for further manipulation/processing. All variables (beyond the first line) are separated by commas.
I've tried the FID=fopen(...) and then read one line at a time, but that's painfully slow as some of the data files are rather large.
X, Y1, Y2, Y3, Txt1, Txt2, Slope
22,17,31.5,Positive,False,-1
24,11,42.5,Flat,False,-1
17,13,33.0,Flat,True,1
...

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 16 de Sept. de 2020
You can use the readtable function. It will autodetect the variable types.
tab = readtable('myfile.txt');
tab = sortrows(tab,{'col1' 'col2'});

Más respuestas (2)

Djamil Boulahbal
Djamil Boulahbal el 16 de Sept. de 2020
Thank you very much Mohammad. Works perfectly.

Djamil Boulahbal
Djamil Boulahbal el 17 de Sept. de 2020
Editada: Djamil Boulahbal el 17 de Sept. de 2020
One more question. When I open the file in a Text Editor, a typical line looks like this
636494112010000000,58.25,1,58.25,20,58.24,87,False,False,1
When I load the same file with the ReadTable function, that same line becomes
6.3649e+17 58.25 1 58.25 20 58.24 87 'False' 'False' 1
The problem is with the very first entry. In the original ASCII file, it is a long integer, but when it's loaded in Matlab, it becomes a double in scientific notation.
The questions is: How do I, when loading the file with ReadTable, ensure the first number loads as an integer. ?
I guess I can convert it afterwards, but is that a fool-proof way ?
That first number (the one I'm trying to keep as integer) is in units of 'ticks' i.e., computer clock cycles

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by