Extract only numbers from a text file

3 ビュー (過去 30 日間)
nanmi51
nanmi51 2018 年 7 月 12 日
回答済み: firas firas 2020 年 12 月 1 日
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

採用された回答

Paolo
Paolo 2018 年 7 月 12 日
編集済み: Paolo 2018 年 7 月 12 日
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 件のコメント
nanmi51
nanmi51 2018 年 7 月 13 日
Hi again. What if I wanted one the two remaining numbers too ?
Paolo
Paolo 2018 年 7 月 13 日
Which of the two? :)

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

その他の回答 (1 件)

firas firas
firas firas 2020 年 12 月 1 日
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)'}

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by