Hi,
I have the following example of string 'E_Field (Farfield) (Cartesian) (-0 -1000 -1)(Abs) (cosim Tran1).txt' .
I need to extract the numbers between the brackets 0,1000,-1.
I have tried using the regexp function but without any success.
I will appriciate any kind of assistance.

답변 (1개)

Chunru
Chunru 2023년 7월 25일
편집: Chunru 2023년 7월 25일

0 개 추천

s = 'E_Field (Farfield) (Cartesian) (-0 -173.648 983.808)(Abs) (cosim Tran1).txt';
s1 = 'E_Field (Farfield) (Cartesian) (-0 -1000 -1)(Abs) (cosim Tran1).txt' ;
p = '(\(-?\d+ -?\d+(.\d*)? -?\d+(.\d*)?\))';
t=regexp(s, p, 'tokens');
t = t{1}{1}
t = '(-0 -173.648 983.808)'
n = sscanf(t, '(%f %f %f)')
n = 3×1
0 -173.6480 983.8080
t1=regexp(s1, p, 'tokens');
t1 = t1{1}{1}
t1 = '(-0 -1000 -1)'
n1 = sscanf(t1, '(%f %f %f)')
n1 = 3×1
0 -1000 -1

댓글 수: 5

EYAL COHEN
EYAL COHEN 2023년 7월 25일
Chunru Hello,
I thank you for your answer but I would like to add that (1) I need all three numbers inbetween the brackets (2) the string changes. For example, it can be as follows 'E_Field (Farfield) (Cartesian) (-0 -173.648 983.808)(Abs) (cosim Tran1).txt'
Chunru
Chunru 2023년 7월 25일
See the updated.
EYAL COHEN
EYAL COHEN 2023년 7월 25일
It seems to isolate the numbers in the brackets for the second example but fails to the first. The numbers are required to be converted to an array of double.
Chunru
Chunru 2023년 7월 25일
See above
EYAL COHEN
EYAL COHEN 2023년 7월 25일
이동: Bruno Luong 2023년 7월 25일
It seems to work, I thank you for your help. A big help.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2023년 7월 25일

이동:

2023년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by