extraction of numeric data with presence of text data

I want to extract x,y co-ordinate and amplitude data from the txt file but i am not getting any idea how to do so.
for eg.:- * Gp# 1[8,4] x,y = -14.46, 0 dT[7] Src Amplitude = 36.0
then i want output as
x y amplitude
1. -14.46 0 36.0
2.
3. and so on

 Accepted Answer

dpb
dpb on 2 Apr 2021
Edited: dpb on 3 Apr 2021
fid=fopen('abc.txt','r');
abc=textscan(fid,'%s',inf,'Delimiter','\n','HeaderLines',2,'commentstyle','1','texttype','string');
fid=fclose(fid);
abc=string(abc{:});
abc=abc(contains(abc,'Gp#'));
xy=str2double(split(extractBetween(abc,'x,y = ',' dT'),','));
ampl=str2double(extractAfter(abc,'Amplitude = '));
tABC=table(xy(:,1),xy(:,2),ampl,'VariableNames',{'x','y','amplitude'});
results in
>> head(tABC)
ans =
8×3 table
x y amplitude
______ ____ _________
-14.46 0.00 36.00
-14.46 0.00 35.00
13.39 0.00 34.00
-18.75 0.00 32.00
19.82 0.00 36.00
-20.89 0.00 32.00
19.82 0.00 50.00
-5.89 0.00 36.00
>>

6 Comments

whenever i am running the program i am getting this error
"Error using contains
First argument must be a string array, character vector, or cell array of character vectors."
how can i solve it? My input file is the same.
Oh...missed copying a line from command window...insert
abc=string(abc{:});
before the contains statement...
also what if i want to extract data below Gp# ie. data in rows 2,4,6,8......
Then you'll have to read the entire file either piecewise w/ textscan between sections or line-by-line and parse each line for being data or the Gp# line and treat accordingly.
NB: you can't use the "trick" I did of treating the leading "1" in data lines as a comment to reduce the file size on input only looking for the other strings to do that.
Probably just readlines to start with the whole file as a string array then contains to find/extract the locations of the headers and processing between them in a loop will be as good as any.
I will try don't know if i can do it since i am very new to matlab but really thanks for the help till this point.
Well, give it a go and post your code and specific Q? if you get stuck...

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 2 Apr 2021

Commented:

dpb
on 5 Apr 2021

Community Treasure Hunt

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

Start Hunting!