How to read data from txt between 2 lines with the same symbol?
조회 수: 6 (최근 30일)
이전 댓글 표시
- Hello!
- I have a lot of .txt files (<1000 lines each). The data format is the following (the picture): there are some lines in the beginning that I don't need, then the line with '*', then the lines with data that I need to extract from the file, then again a line with '*' and some comments that I don't need.
- Is there any way to do that? I have a lot of such files. The matter is that in every file the number of lines before the first '*' is different. So, is there any way to read the data in between of two '*'? I tried all the functions but I am a beginner and just cannot come up with the right idea...

댓글 수: 0
채택된 답변
dpb
2017년 10월 11일
That's pretty simple to do, actually...
fid=fopen('yourfile.txt'); % open the file
while ~feof(fid) % begin loop, we'll break later...
l=fgetl(fid); % read a record into character variable
if strfind(l,'*'), break, end % found the first '*' so quit...
end
data=cell2mat(textscan(fid,repmat('%f',1,4),'collectoutput',1));
The last will read records of four numeric values until it runs out of data or there's a conversion error (which there will be at the next '*' record. But, that's ok, that's all you wanted... :)
To do multiple files, you dir with a suitable filename wildcard pattern to return the list of files and iterate over it.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!