6

In Matlab, if I have a cell array of strings, how can I check if for example 3rd row and 1st column is equal to some given string, for example 'ABC'?

For example, myArray(3,1) == 'ABC' gives me an error:

Undefined operator '==' for input arguments of type 'cell'.

Error in cw14 (line 19)
if myArray(3,1) == 'ABC'

1 Answer 1

12

That's because you need to use {curly braces} in order to access the content of the cell array.

Using (regular brackets) indexes the actual cell, which in your case contains a string. Moreover, in order to check for the presence of strings I recommend using strcmp or maybe strfind.

Therefore use this:

strcmp(myArray{3,1},'ABC')

Check here for infos about indexing into cell arrays.

EDIT (following comments)

Using logical == in order to find for strings into a cell array is not safe, because using this operator splits the strings and compares every letter forming it, as opposed to strcmp and the likes, that checks the whole string.

Consider this code, where I put some strings into myArray:

myArray = {'A' 'B' 'ABC' 'CBA' 'ABC'}.'

myArray = 

    'A'
    'B'
    'ABC'
    'CBA'
    'ABC'

If we apply == on this cell array of strings as follows:

Check_31 = myArray{3,1} == 'ABC'

Check_41 = myArray{4,1} == 'CB_'

Matlab returns those 2 logical vectors:

Check_31 =

     1     1     1


Check_41 =

     1     1     0

So as you see, the character "_" is not the last element of the string present in the cell {4,1}.

Now if we use strcmp (on the whole cell array; we don't need to index specific cells to check whether some string is present):

Check_ABC = strcmp(myArray,'ABC')

We also get a logical vector, but this time not referring to the 3 letters forming the string inside the cell, but rather referring to the cell array itself and whether 'ABC' is present or not. The result is this:

Check_ABC =

     0
     0
     1
     0
     1

Which makes sense, since 'ABC' is indeed present in cells {3,1} and {5,1}.

Hope that's clearer!

4
  • myArray{3,1} == 'ABC' works fine, but if I do it in a loop, for example myArray{i,1} == 'ABC' (looping over i) it doesn't. It says matrix dimensions must agree. Why it doesn't work and why we have to use strcmp here?
    – user107986
    Commented May 11, 2015 at 10:15
  • What about strcmp(myArray{i,1},'ABC')?
    – JaBe
    Commented May 11, 2015 at 10:17
  • 3
    With == you test every element of the character array {i,1} with all elements of the right hand side character array 'ABC'. To compare 'XYZ' == 'ABC' gives [0 0 0] but 'XY' == 'ABC' gives the error, because the dimensions don't agree (2 not the same as 3).
    – JaBe
    Commented May 11, 2015 at 10:24
  • Please see my edited answer. Thanks @JaBe haha that's exactly it :)
    – Benoit_11
    Commented May 11, 2015 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.