Extracting data from text file only between certain words

15 vues (au cours des 30 derniers jours)
Zain
Zain le 18 Juin 2022
Commenté : Zain le 20 Juin 2022
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.

Réponse acceptée

Image Analyst
Image Analyst le 18 Juin 2022
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);

Plus de réponses (2)

Damir
Damir le 19 Juin 2022
Modifié(e) : Image Analyst le 19 Juin 2022
A PDF file explaining extraction you have asked for :
  1 commentaire
Image Analyst
Image Analyst le 19 Juin 2022
Unless he wants to translate from Prolog I think you'll have to explain how to run Prolog code from within MATLAB.

Connectez-vous pour commenter.


Damir
Damir le 19 Juin 2022
No, this was not MATLAB related answer.
Just data extraction.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by