Can't find a string inside a cell array
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi,
I have a cell array of 30 lines x 1 column. Its name is onlyFirstColumn, and this is it's content:
'IMG10_N6.mat'
'IMG13_N5_5.mat'
'IMG11_N5_1.mat'
'IMG14_N6_2.mat'
'IMG1_N2_3.mat'
'IMG3_N1.mat'
'IMG4_N3_4.mat'
'IMG5_N2.mat'
'IMG7_N3.mat'
'IMG8_N4.mat'
'IMG9_N7.mat'
'IMG11_N6_4.mat'
'IMG12_N4_2.mat'
'IMG12_N4_3.mat'
'IMG14_N6_1.mat'
'IMG1_N1_1.mat'
'IMG1_N2_4.mat'
'IMG2_N1_4.mat'
'IMG6_N3_2.mat'
'IMG6_N3_3.mat'
'IMG11_N5_2.mat'
'IMG12_N4_1.mat'
'IMG14_N6_3.mat'
'IMG2_N1_2.mat'
'IMG4_N2_1.mat'
'IMG13_N5_3.mat'
'IMG13_N5_4.mat'
'IMG4_N2_2.mat'
'IMG6_N3_1.mat'
'IMG2_N1_3.mat'
I have another variable called imgConsult, which is a cell array of 1 x 1 (1 line, 1 column, I mean: it has only one value). The value of this cell array is
imgConsult =
'IMG10_N6.mat'
I'm trying to find imgConsult in that cell array, by using this code:
IndexImage = find(strcmp(onlyFirstColumn, imgConsulta));
However, this IndexImage returns nothing ([]). That's strange because when I use ismember function, it returns 1 (true). I've tried this for hours until I finally decided to come here for help, as this is driving me crazy.
Can you please give me some ideas of what is going on or what am I doing wrong?
Thank you in advance!
댓글 수: 1
dpb
2017년 3월 17일
Works fine here...
>> a={'IMG10_N6.mat'
'IMG13_N5_5.mat'
'IMG11_N5_1.mat'
'IMG14_N6_2.mat'
'IMG1_N2_3.mat'
'IMG3_N1.mat'
'IMG4_N3_4.mat'
'IMG5_N2.mat'
'IMG7_N3.mat'
'IMG8_N4.mat'}; % just a part of the array is enough...
>> imgConsult = 'IMG10_N6.mat';
>> find(strcmp(a,imgConsult))
ans =
1
>>
Note, however, the strcmp is exact compare so if in reality you've got something like
>> imgConsult = 'IMG10_N6.mat ' % an extra trailing blank, maybe?
imgConsult =
IMG10_N6.mat
>> find(strcmp(a,imgConsult))
ans =
Empty matrix: 0-by-1
>>
it fails.
Something like that is about all I can think of at the moment...need a reproducible test case that can run that illustrates the symptom I think...
채택된 답변
Queila Martins
2017년 3월 17일
댓글 수: 1
Guillaume
2017년 3월 17일
Well, if that works that's because OnlyFirstColumn is a cell array of cell arrays of char array as opposed to a cell array of char arrays. That is, each cell in OnlyFirstColumn is itself a cell array (probably 1x1).
You would get the same answer with:
find(strcmp([OnlyFirstColumn{:}], imgConsult))
What fixes the problem is the concatenation of all these sub 1x1 cell arrays into just one 1xn cell array, not the switch to ismember.
추가 답변 (2개)
Jan
2017년 3월 17일
Works for me also for both versions:
imgConsult = {'IMG10_N6.mat'}
imgConsult = 'IMG10_N6.mat'
Your posted code contains "imgConsulta" instead of "imgConsult". Perhaps a typo?
댓글 수: 2
dpb
2017년 3월 17일
As said above, try cut 'n paste a selection from command window to the forum; if can't see it, we can't try to duplicate it. HAS to somehow be in the data or some other usage error.
Oh, just for grins, try
clear find strcmp
and try again just in case there's an aliasing problem...would seem unlikely if so and would even run, but all ports in a storm.
Guillaume
2017년 3월 17일
As others have said, you must be doing something wrong somewhere that you're not showing us.
However, since you say that ismember return true, you could just get its second return value at the same time. That's the position of the string in the cell array (1st index only if it appears multiple time, however):
[found, where] = ismember(imgConsult, a);
ismember uses strcmp internally, so of course, there's no reason for it to return a different answer than find(strcmp).
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!