1

This code:

if (prefixTree(1,4).prefixTree2(:,2)=='2')
    unique(prefixTree(1,4).prefixTree2(:,3))
end

returns this error:

Undefined function 'eq' for input arguments of type 'cell'.

Why?

5
  • you might consider adding matlab as a tag in order to gain visibility to your question. Commented Jan 12, 2016 at 15:24
  • 2
    What do you expect the if statement to do? prefixTree(1,4).prefixTree2(:,2) looks like that it's a cell array, but your comparison doesn't make any sense at all. In plain English, what is your if statement doing? Take a step backward. Commented Jan 12, 2016 at 15:37
  • the error is specifically pointing to the part where you have prefixTree2(:,2)=='2' Matlab does not allow == for cell comparing, and also '2' would indicate that it is a String type? Try removing the ' ' and see what error it throws? Commented Jan 12, 2016 at 15:38
  • I expect that if prefixTree(1,4).prefixTree2(:,2) is equal to 2, the code find the unique values in prefixTree(1,4).prefixTree2(:,3). @GameOfThrone i have tried without '' but the code gives me the same error. Commented Jan 12, 2016 at 15:43
  • 1
    In which case, I think horchler has got the right answer, which uses strcmp the string compare function that can be applied to indexed cell and compare its content with another string. Commented Jan 12, 2016 at 15:46

1 Answer 1

5

The error implies that prefixTree(1,4).prefixTree2(:,2) is a cell array. You can access the individual elements of the second column with prefixTree(1,4).prefixTree2{:,2}. Also, the colon operator implies that there is more than one element in prefixTree(1,4).prefixTree2(:,2) but you're trying to do a scalar comparison. Lastly, you're comparing to a char ('2' as opposed to the number 2) and thus it would be best to use string functions. You can use strcmp to check each element of your cell:

prefixTree(1,4).prefixTree2 = {'1' '2';'3' '2'}; % Example data
strcmp(prefixTree(1,4).prefixTree2(:,2),'2')

Then use any or all to return a scalar for your if statement:

if all(strcmp(prefixTree(1,4).prefixTree2(:,2),'2'))
    ...
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.