I have a text file something similar to below.
Time
10
Atoms
100
1 2 .1 .3 .4
2 1 .4 .6 .7
3 1 .5 .6 .8
Time
20
Atoms
100
1 2 .5 .6 .4
2 1 .4 .6 .9
3 1 .5 .6 .4
Time
30
Atoms
100
1 2 .5 .6 .4
2 1 .4 .7 .9
3 1 .7 .6 .1
Time
............
............
The text file is repeated for many times. Now I want to extract the data which is in the format of '%f%f%f%f%f'. i.e I need to generate an matrix as below.
[1 2 .1 .3 .4
2 1 .4 .6 .7
3 1 .5 .6 .8
1 2 .5 .6 .4
2 1 .4 .6 .9
3 1 .5 .6 .4
1 2 .5 .6 .4
2 1 .4 .7 .9
3 1 .7 .6 .1
............
...........]
How to do that without using loop?
Thanks

 채택된 답변

Star Strider
Star Strider 2016년 11월 24일

0 개 추천

It’s not possible to do it without a loop. The reason is that you have to do something similar to:
fidi = fopen( ... ,'rt');
k1 = 1;
while ~feof(fidi)
D{k1} = textscan(fidi, '%f%f%f%f', 'HeaderLines',4, 'CollectOutput',true);
fseek(fidi, 0, 0)
k1 = k1 + 1;
end
fclose(fidi)
The code will stop when it hits a text line, then with the fseek call it will advance to the next line, skip it and the next 3 header lines, then continue through until it encounters a valid ‘end-of-file’ indicator, then stop. If there is no valid ‘end-of-file’ indicator, this becomes an infinite loop, so be mindful of that possibility.
NOTE This is UNTESTED CODE. It should work with appropriate modifications. You may need to add additional name-value pair arguments to the textscan call to make it work with your file.

댓글 수: 2

Nalaka Samaraweera
Nalaka Samaraweera 2016년 11월 24일
Excellent, Thank you very much
Star Strider
Star Strider 2016년 11월 24일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

질문:

2016년 11월 23일

댓글:

2016년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by