"contains" with multiple conditions

63 次查看(过去 30 天)
Hi, I have the following code
A = "159 ( 51,1%) 13 (4,2% ) 139 (44,7%)";
A = textscan(A,'%s','Delimiter',' ')';
A{:}(contains(A{:},'(')) = [];
A{:}(contains(A{:},')')) = [];
A{:}(contains(A{:},'%')) = [];
which removes all the brackets and the numbers between the brackets (in this case they are percentages):
% Result after "textscan" (just typing A{:})
8×1 cell array
{'159' }
{'(' }
{'51,1%)' }
{'13' }
{'(4,2%' }
{')' }
{'139' }
{'(44,7%)'}
% Result after all the "contains" (just typing A{:})
3×1 cell array
{'159'}
{'13' }
{'139'}
However, I would like to use one single command for contains with multiple conditions. Would it be possible?
Something like this:
% Input
A{:}(contains(A{:},['(' OR ')' OR '%')) = [];
% Desired output
A = "159 13 139";

采纳的回答

Matt J
Matt J 2021-12-16
编辑:Matt J 2021-12-16
A = "159 ( 51,1%) 13 (4,2% ) 139 (44,7%)";
A=eraseBetween(A,"(", ")")
A = "159 () 13 () 139 ()"
cellstr( extract(A,digitsPattern) )
ans = 3×1 cell array
{'159'} {'13' } {'139'}
  2 个评论
Sim
Sim 2021-12-16
Many thanks @Matt J .... but actually I would need to remove the numbers among the brackets..
My desired output would be this one:
% Desired output
A = "159 13 139";

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2021-12-16
编辑:Matt J 2021-12-16
A = "159 ( 51,1%) 13 (4,2% ) 139 (44,7%)";
A = textscan(A,'%s','Delimiter',' ')';
A=vertcat(A{:});
A( contains(A,{'(',')','%'}) )=[]
A = 3×1 cell array
{'159'} {'13' } {'139'}
  1 个评论
Sim
Sim 2021-12-16
Oh Great!! This is exactly what I was thinking about/looking for! The multiple conditions in "contains" are given by curly brackets..!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by