Create variable from cell array based on strings

조회 수: 8 (최근 30일)
Hillaryfor2016
Hillaryfor2016 2015년 4월 2일
댓글: Hillaryfor2016 2015년 4월 2일
I have a cell array something like this...
if true
Var1=
'A' 6
'B' 7
'B' 7
'A' 8
'C' 10
'A' 9
'A' 3
end
I want to pull out the values into a new variable which correspond only to 'A'
I.e generate this variable
if true
Var2=
'A' 6
'A' 8
'A' 9
'A' 3
end

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 4월 2일
Var1={'A',6; ...
'B',7; ...
'B',7; ...
'A',8; ...
'C',10; ...
'A',9; ...
'A',3};
Var2=Var1(strcmpi(Var1(:,1),'A'),:)
Var2 =
'A' [6]
'A' [8]
'A' [9]
'A' [3]

추가 답변 (1개)

Thomas Koelen
Thomas Koelen 2015년 4월 2일
편집: Thomas Koelen 2015년 4월 2일
I think this should work. I am on mobile though so I can't check it atm.
index=strcmp(var1(:,1),'A');
index=index(:,any(index));
var2=var1(index,:);
  댓글 수: 1
Hillaryfor2016
Hillaryfor2016 2015년 4월 2일
Cheers, haven't tried it but thanks for the help! (The 1st answer worked fine!)

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

카테고리

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