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 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
Geoff Hayes
2019년 2월 15일
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
Geoff Hayes
2019년 2월 15일
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?).
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!