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!
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!