필터 지우기
필터 지우기

Extract only numbers from a text file

조회 수: 2 (최근 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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by