필터 지우기
필터 지우기

STRCMP in a loop

조회 수: 6 (최근 30일)
Jared
Jared 2013년 2월 27일
I have a variable number of strings stored in an array. I want to compare each of those strings with a cell, and if the cell does not match any of the strings in the array, I want to delete it.
I'm not quite sure of the syntax. I know I can find the number by using length, but I can not do something like:
for j=1:length(match)
for i=1:length(compare_strings)
if strcmp(compare_strings(i),match(j))==0
compare_strings(i)=[];
end
end
end
It need to be more along the lines of:
if (strcmp(compare_strings(i),match(1))==0 && strcmp(compare_strings(i),match(2))==0 && strcmp(compare_strings(i),match(3))==0)
compare_strings(i)=[];
end
I need to check all of them at once, otherwise I may delete something that doesn't match the first, but matches one of the next. Any thought on the approach?

채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 27일
Use ismember()
  댓글 수: 1
Muthu Annamalai
Muthu Annamalai 2013년 2월 27일
You can also use strcmp() with cell strings which will eliminate the loop. http://www.mathworks.com/help/matlab/ref/strcmp.html

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 27일
Look at this example
first_liste={'vert','jaune','rouge','azure','bleu'}
second_liste={'verte','noire','cerise','rouge','bleu'}
[ii,jj]=ismember(first_liste,second_liste)
second_liste=second_liste(jj(jj>0))

카테고리

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