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.
Untitled.png
  댓글 수: 3
Chen Wang
Chen Wang 2019년 4월 25일
Hi dpd,
Thank you so much for your quick reply!
The situation is I want all the lines of this type. There are thousands of such data lines. Also the loacation of these lines are not fixed. In this case, would you please be more specific about what commands could do such job?
Thank you so much!
dpb
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
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
Chen Wang
Chen Wang 2019년 4월 25일
Hi,
Thank you so much for your help. I used exactly the codes you provided and it worked! To make it more clear for others who might have the same problems, I also attached the .txt file on which this code worked. Thank you again!
dpb
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 CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by