Remove simultaneously several substrings in a string

Hi, I have a string
A = "159 (51,1%) 13 (4,2%) 139 (44,7%)";
and I would need to remove all the numbers among brackets (and brackets included!). My goal is to get this new string:
A = "159 13 139";
Any idea ?
I tried this, but it is not working:
>> strtok(A,'()')
ans =
"159 "

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 12월 16일
hello
try this
A = "159 (51,1%) 13 (4,2%) 139 (44,7%)";
B = split(A,' ');
B(contains(B,'(')) = [];
B = join(B,' ');

댓글 수: 3

it works also with
B(contains(B,'%')) = [];
or
B(contains(B,')')) = [];
Sim
Sim 2021년 12월 16일
편집: Sim 2021년 12월 16일
Many thanks, it work very well!
...Just for fun and curiosity, I also tried textscan with your solution..
B = textscan(A,'%s','Delimiter',' ')';
B{:}(contains(B{:},'(')) = []
B = join(B{:},' ')
which gives:
B =
1×1 cell array
{'159 13 139'}
My pleasure ,
yes , sure , there are probably even other possibilities

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

추가 답변 (0개)

카테고리

질문:

Sim
2021년 12월 16일

댓글:

2021년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by