I am trying to check if a value of a vector is equal, but it is a word not a number. how can I do that?

1 vue (au cours des 30 derniers jours)
Hi,
I have a vector >> t = {'add','23','27','pow','3','2','2','stop','4','stop', ...
% 'mult','add','2','3','stop','add','4','5','stop','stop', ...
% 'div','2','3','4','stop', ...
% 'sub','4','2','stop', ...
% 'mult','2','3','div','2','stop','add','4','5','2', ...
% 'mult','1','3','stop','stop','3','stop'}
and I am trying to set an if condition for when t(x)= 'stop' but I keep getting an error that says Undefined operator '==' for input arguments of type 'cell'. If you could tell\help me with how to do this I would greatly appreciate it!

Réponses (1)

Geoff Hayes
Geoff Hayes le 15 Fév 2019
Ashley - how are you iterating over your cell array? It looks like all of your elements are strings (character arrays) so you should be using either strcmp or strcmpi to compare two strings. For example,
k = 2;
if strcmp(t{k}, 'stop')
% do something
end
If you use == you can get errors when the two strings that you are comparing are of different dimension.
  4 commentaires
Geoff Hayes
Geoff Hayes le 15 Fév 2019
Ashley - try using strcmp....it looks like you are using = (assignment) instead which will not work...and you must wrap your string in single quotes. Consider
if strcmp(expr{i}, 'stop')
Note how we use the {} to access the element in the cell array (I'm assuming this is still the case?).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by