フィルターのクリア

Can't find a string inside a cell array

10 ビュー (過去 30 日間)
Queila Martins
Queila Martins 2017 年 3 月 17 日
コメント済み: Guillaume 2017 年 3 月 17 日
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
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
Queila Martins 2017 年 3 月 17 日
Based on Guillaume's answer, I tried a code that got me what I needed:
find(ismember([onlyFirstColumn{:}], imgConsult));
Thank you all for your valuable help!
  1 件のコメント
Guillaume
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
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 件のコメント
Queila Martins
Queila Martins 2017 年 3 月 17 日
Sorry, it's a typo here, as I changed the variable name only here because it was in portuguese. It's really strange, it does not work for me, I thought the problem would be the way I was using the functions. No idea what's going on :/
dpb
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
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).
  1 件のコメント
Queila Martins
Queila Martins 2017 年 3 月 17 日
Hi!!
Guillaume, your code help me on finding the right code for what I need: indexImage = find(ismember([onlyFirstColumn{:}], imgConsult));
Thank you so much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by