MATLAB Answers

0

How to extract the data lines from a large text file which contains combined numeric lines and text lines

Asked by Chen Wang on 25 Apr 2019
Latest activity Commented on by dpb
on 25 Apr 2019
Accepted Answer by dpb
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 Comments

If the size of the file is fixed so the location of the target line is the same, you can use testxcan with the headerlines optional named parameter.
If, as one would presume, the output is dependent upon the input and so isn't necessarily the same exact number of lines, then one has to read through the file finding a unique text string (or sequential set of strings) that identifies the particular information wanted...for example, is the result wanted only the one line highlighted or all lines of that type for all the SECTION NAME sections in the file, or ...?
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!
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.

Sign in to comment.

1 Answers

Answer by dpb
on 25 Apr 2019
Edited by dpb
on 25 Apr 2019
 Accepted Answer

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 Comments

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!
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.

Sign in to comment.



Translated by