How to extract the data lines from a large text file which contains combined numeric lines and text lines
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello everyone,
I want to extract the data line in a large text file as shown in the picture. The data lines are just between a few lines of text as hilighted with the red box. I have tried the commands below:
fileID=fopen('OneLineData.dat');
C=fscanf(fileID, '%f %f %f %f');
fclose(fileID)
But it does not work. Can anyone tell me how to deal with this issue? Any of your help is greatly appreciated.
댓글 수: 3
dpb
2019년 4월 25일
Be more helpful if you would attach a portion of a file so folks could test drive...but I'll post a basic outline of the solution from the file; it's also possible if the files aren't too large to read the full file as byte/char array and do the selection in memory.
채택된 답변
dpb
2019년 4월 25일
편집: dpb
2019년 4월 25일
Basic outline is as follows...
fid=fopen('OneLineData.dat');
data=[];
l=fgetl(fid);
while ~feof(fid)
if contains(l,'SOFM SOF1 SOF2 SOF3');
data=[data,textscan(fid,repmat('%f',1,4),'headerlines',1);
end
l=fgetl(fid);
end
fid=fclose(fid);
댓글 수: 2
dpb
2019년 4월 25일
We all get lucky now and then! :) Glad to help; sometimes things that seem hard really aren't so much when one first sees the "trick"...
What you have to watch out for in such cases is that the specific text you search for is, indeed, actually unique to the data you're trying to find.
추가 답변 (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!