Extracting data from text file only between certain words

조회 수: 12 (최근 30일)
Zain
Zain 2022년 6월 18일
댓글: Zain 2022년 6월 20일
I have a really big text file with a ton of data. I only want some of the information. In this case, it's grid properties, which is irrelevant to the question. I want to disregard everything in the text file except the data between the words "BOUNDARY GRID DATA" and "SPOINT DATA." It looks like this essentially:
BOUNDARY GRID DATA
GRID* 5250001 5200000 0.730198500E+02-0.177000000E+03
* -0.512716000E+02 5200000 0 0
GRID* 5250002 5200000 0.730198500E+02-0.176250000E+03
* -0.512716000E+02 5200000 0 0
GRID* 5250003 5200000 0.730198500E+02-0.175500000E+03
* -0.512716000E+02 5200000 0 0
GRID* 5250004 5200000 0.730198500E+02-0.174750000E+03
* -0.512716000E+02 5200000 0 0
%so on for many more grids
SPOINT DATA
So I'm struggling to figure out a good way to open this text file and only extract this data. The data for each grid is also in two rows, so I also have to worry about that, but I think I can work through that. The data extraction is the biggest problem. Thanks in advance!
Edit: This is what I have so far
filename = 'Fairing2_FS.asm';
fid1 = fopen(filename);
fid2 = fopen('grid_points.txt','w+');
tline = fgets(fid1); % read input file line by line
idx=1;
while ischar(tline) %problem is in this line
if size(tline,2)>73
if strcmpi(tline(3:20), 'BOUNDARY GRID DATA')
fprintf(fid2,'%s',tline);
idx_start = idx
chkstr = [tline ' '];
while ~strcmpi(chkstr(3:13),'SPOINT DATA')
fprintf(fid2,'%s',tline);
tline = fgets(fid1);
idx = idx+1;
chkstr = [tline ' '];
end
fprintf(fid2,'%s',tline);
idx_end = idx
break;break;break
else
tline = fgets(fid1);
idx = idx+1;
end
else
tline = fgets(fid1);
idx = idx+1;
end
end
fclose(fid1);
fclose(fid2);
I know I'm close, but fgets continues to return -1 where I noted the problem is. I know that means the end of the file has been reached, but I'm not sure how to fix that.

채택된 답변

Image Analyst
Image Analyst 2022년 6월 18일
Why not simply use fileread to suck up the whole file into a single string, then use strfind() to find the starting and ending indexes
str = fileread(filename)
index1 = strfind(str, 'BOUNDARY GRID DATA');
index2 = strfind(str, 'SPOINT DATA'
outputString = str(index1:index2);

추가 답변 (2개)

Damir
Damir 2022년 6월 19일
편집: Image Analyst 2022년 6월 19일
A PDF file explaining extraction you have asked for :
  댓글 수: 1
Image Analyst
Image Analyst 2022년 6월 19일
Unless he wants to translate from Prolog I think you'll have to explain how to run Prolog code from within MATLAB.

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


Damir
Damir 2022년 6월 19일
No, this was not MATLAB related answer.
Just data extraction.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by