Extracting data from text file only between certain words
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (2)
Damir
il 19 Giu 2022
Modificato: Image Analyst
il 19 Giu 2022
0 voti
A PDF file explaining extraction you have asked for :
1 Commento
Image Analyst
il 19 Giu 2022
Unless he wants to translate from Prolog I think you'll have to explain how to run Prolog code from within MATLAB.
Damir
il 19 Giu 2022
0 voti
No, this was not MATLAB related answer.
Just data extraction.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!