Read and sort data in a text file

7 ビュー (過去 30 日間)
Djamil Boulahbal
Djamil Boulahbal 2020 年 9 月 16 日
編集済み: Djamil Boulahbal 2020 年 9 月 17 日
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
...

採用された回答

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

その他の回答 (2 件)

Djamil Boulahbal
Djamil Boulahbal 2020 年 9 月 16 日
Thank you very much Mohammad. Works perfectly.

Djamil Boulahbal
Djamil Boulahbal 2020 年 9 月 17 日
編集済み: Djamil Boulahbal 2020 年 9 月 17 日
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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by