Extracting data from text file only between certain words

11 ビュー (過去 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);
  1 件のコメント
Zain
Zain 2022 年 6 月 20 日
Worked perfectly thank you!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by