I Want to make a program that can respond back to you with words. I found one way but its annoying. I need to put (' ') Around the words I type and the it response but I just want to type the words and it give off a correct responses. Any good examples to make this work?

 採用された回答

Walter Roberson
Walter Roberson 2013 年 9 月 15 日

0 投票

Use input with the 's' option to request the responses
din = input('What did you have for dinner?', 's');

7 件のコメント

Rainaire Hansford
Rainaire Hansford 2013 年 9 月 20 日
Not exactly what i meant. What I really was looking for was if I say hello, hey or hi, it will give me a respond for either of them. For Example I have din = input(' ', 's'); if din <= 'Hello' fprintf('Hello sir'); if din <= 'Hi' fprintf('Hi there'); end end. Now it works for the first one (Hello) but when I type hi it say something like matrix error or some stuff. I want it to work for both.
Matt Kindig
Matt Kindig 2013 年 9 月 20 日
編集済み: Matt Kindig 2013 年 9 月 20 日
Don't use the <= operator. The reason you get the matrix error is because din cannot have both 5 characters (as it does if din=='Hello') and 3 characters (as if din=='Hi'), as your code assumes. That is the source of your matrix size error.
Use strcmp() (or strcmpi()) instead:
if strcmpi(din, 'Hello'),
fprintf('Hello sir');
elseif strcmpi(din, 'Hi'),
fprintf('Hi');
end
Rainaire Hansford
Rainaire Hansford 2013 年 9 月 25 日
Thanks guys it work now let say I want to make it respond to any word for ex.
if strcmpi(din, 'Hello','Hi','Whats up')
This obiviously doesnt work but I think you understand when I mean that. Like if i make any of these responds it will send back the same responds. How could i do that
Walter Roberson
Walter Roberson 2013 年 9 月 25 日
if strcmpi(din, {'Hello', 'Hi', 'Whats up'})
Rainaire Hansford
Rainaire Hansford 2013 年 9 月 29 日
Yeah that's not working like I planned any other suggestions.
Walter Roberson
Walter Roberson 2013 年 9 月 30 日
if any(strcmpi(din, {'Hello', 'Hi', 'Whats up'}))
Rainaire Hansford
Rainaire Hansford 2013 年 9 月 30 日
Awesome Thanks alot

サインインしてコメントする。

その他の回答 (1 件)

Rainaire Hansford
Rainaire Hansford 2013 年 9 月 20 日

0 投票

How do I make if and else statement equal to the words that I type.

カテゴリ

ヘルプ センター および File Exchange で Language Support についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by