How to make a string == one string in a cell array

I need to make a chatbot for an assignment and im having trouble with this part of the script
x = input('+ ', 's');
if x == {'hey','hi','hello'};
disp('Hello')
So if the user says hi, hey or hello, the script will print hello.
But when I type in, for example, 'hey' I get the error
Undefined function 'eq' for input arguments of type 'cell'.
Error in Bot (line 3) if x == {'hey','hi'};
Cheers

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 5 月 24 日

0 投票

x = input('+ ', 's');
s={'hey','hi','hello'};
if ismember(x,s)
disp('Hello')
end

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 24 日

0 投票

x = input('+ ', 's');
if any(strcmpi(x, {'hey','hi','hello'})
disp('Hello')
end
You should avoid using "==" to compare strings. "==" will fail if the two strings have different lengths. Use strcmp() or strcmpi() instead

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2015 年 5 月 24 日

回答済み:

2015 年 5 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by