How replace matching string

조회 수: 1 (최근 30일)
Mekala balaji
Mekala balaji 2019년 6월 12일
댓글: Stephen23 2019년 6월 28일
Hi,
I have below cell array:
Pass Pass{overflow :1,unbound:5,warning:9} Miss
Pass Miss Trigger
Passs Pass Pass
I want to replace:
  1. Pass with '1',
  2. Miss with '2' ,
  3. Any thing else with '2'
Kindly some one help how to do this,
I can only strrep to replace, I dont not know how to do 3rd condition(any thing else)

답변 (1개)

Rik
Rik 2019년 6월 12일
편집: Rik 2019년 6월 12일
Because your second and third condition can be merged, you can use ismember:
data={'Pass','Pass{overflow :1,unbound:5,warning:9}','Miss';
'Pass','Miss','Trigger';
'Passs','Pass','Pass'};
L=ismember(data,{'Pass'});
output=2*ones(size(data));
output(L)=1;
Note that this requires exact matches. You could also write a custom function and use cellfun:
output2=cellfun(@my_custom_fun,data)
function val=my_custom_fun(str)
if strcmpi(str,'pass')
val=1;
elseif strcmpi(str,'miss')
val=2;
elseif contains(str,'Pass')
val=1.5;
else
val=2;
end
end
  댓글 수: 3
Mekala balaji
Mekala balaji 2019년 6월 28일
It giving me error,
Error using cellfun
Undefined function 'my_custom_fun' for input arguments of type 'char'.
Stephen23
Stephen23 2019년 6월 28일
@Mekala balaji: did you copy the function that Rik gave you, and save it to your Search Path (e.g. the current directory) using that function name?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by