How to read a certain part of data from a very complicated text file containing many empty lines

2 ビュー (過去 30 日間)
Hi, the text file includes several data structures, delimited by empty lines, like the following: -----------------------------------------
Empty line...
Empty line...
X Y Z
0.2 0.3 0.4
0.2 0.3 0.4
0.2 0.3 0.4
Three Empty lines...
A B C D E F G
Empty line...
0.1 0.2 0.3 0.4 0.5 0.6 0.7
Four Empty lines...
F R I S T G R O U P L O A D I N G
Empty line...
YY YY*C
-0.9 0.154
-0.8 0.023
-0.7 0.087
-0.6 0.098
Empty lines... Other complicated data forms...
-------------------------------------------------
I need only the YY YY*C part, to read the data into a matrix. I've tried textscan and fgetl, neither work since textscan only read one type of data structure,the data format I want is not very distinct from others; and fgetl tend to take empty lines as end of file marker, so it stops at empty lines.
Does anyone has any idea? Thanks a lot since this is very difficult for me to figure it out!

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 8 日
Hi, Pengfei
Let say that I have a text file formated :
X Y Z
0.2 0.3 0.4
0.2 0.3 0.4
0.2 0.3 0.4
A B C D E F G
0.1 0.2 0.3 0.4 0.5 0.6 0.7
F R I S T G R O U P L O A D I N G
YY YY*C
-0.9 0.154
-0.8 0.023
-0.7 0.087
-0.6 0.098
Here my code :
fid = fopen('data.txt');
idk = 0;
tline = fgetl(fid);
while ischar(tline)
idk = idk + 1;
data01{idk} = tline;
tline = fgetl(fid);
end
fclose(fid);
idx = 1;
for x = 19 : 22
data02{idx} = textscan(data01{x},'%f %f');
idx = idx + 1;
end
I succesfully reading your data by knowing that
YY YY*C is in line 18, so I start reading from line 19
to 22.
  1 件のコメント
Pengfei
Pengfei 2012 年 1 月 8 日
Hi Chandra, Thanks so much!
Looks like I was wrong to take empty lines as end of file marker...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by