Extract only numbers from a text file

I have a text file that contains a series of lines where each line is of the following form: s.CircleByCenterPerimeter(center=(9.7593, 11.2643), point1=(9.8851, 11.2643))
I would like to extract just the first and third numbers from this line of text, and therefore from each line in the text file.
I tried a couple of things using textread(), but I couldn't figure out how to do it.
Any input would be much appreciated. Thank you

 Akzeptierte Antwort

Paolo
Paolo am 12 Jul. 2018
Bearbeitet: Paolo am 12 Jul. 2018

0 Stimmen

str = 's.CircleByCenterPerimeter(center=(9.7593, 11.2643), point1=(9.8851, 11.2643))';
regexp(str,'(?<=\()-?\d+\.?\d+','match')
For your text file:
raw = fileread('yourfile.txt');
data = regexp(raw,'(?<=\()-?\d+\.?\d+','match')
data = reshape(data,2,[]);

7 Kommentare

nanmi51
nanmi51 am 12 Jul. 2018
That did it!! Thank you so much Paolo.
Paolo
Paolo am 12 Jul. 2018
You are welcome :)
nanmi51
nanmi51 am 12 Jul. 2018
Just one last question, it seems like if those two numbers were negative, the output is empty. I tried the following line, and the result was an empty cell.
s.CircleByCenterPerimeter(center=(-11.7509, 5.2936), point1=(-11.6094, 5.2936))
Paolo
Paolo am 12 Jul. 2018
I've updated the answer. Also note that if you need to do numerical calculations with the data you will need to convert data to double using str2double.
nanmi51
nanmi51 am 12 Jul. 2018
Awesome, that worked. Yes, I will need to subtract the two numbers, and the result represents the radius of the circle.
nanmi51
nanmi51 am 13 Jul. 2018
Hi again. What if I wanted one the two remaining numbers too ?
Paolo
Paolo am 13 Jul. 2018
Which of the two? :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

firas firas
firas firas am 1 Dez. 2020

0 Stimmen

You could include the other two numbers as well:
regexp(str, '\d+?\.?\d+(?=))|(?<=\()\d+\.?\d+' , 'match')
ans =
1×4 cell array
{'9.7593'} {'11.2643)'} {'9.8851'} {'11.2643)'}

Kategorien

Gefragt:

am 12 Jul. 2018

Beantwortet:

am 1 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by