Need help inputing words

2 Ansichten (letzte 30 Tage)
Samy Ben Thabet
Samy Ben Thabet am 9 Okt. 2018
Kommentiert: Image Analyst am 9 Okt. 2018
I want to ask my user to input a shape:
h=input('What shape do you want?:');
if h==square;
disp(h)
But this never works, Matlab shows me the following error message: "'square' requires Signal Processing Toolbox."
Can someone help me find a way to make this work please :)

Akzeptierte Antwort

Star Strider
Star Strider am 9 Okt. 2018
You need to add the 's' to the input argument list, and then use strcmp for the comparison.
Try this:
h=input('What shape do you want?:', 's');
if strcmp(h, 'square')
sprintf('SQUARE!')
end
  1 Kommentar
Image Analyst
Image Analyst am 9 Okt. 2018
If you can't use contains() like in my answer because your version of MATLAB is too old, then you can make this more robust by using strcmpi() instead of strcmp() and use strtrim() in case the user put any leading or trailing spaces on their response:
h = input('What shape do you want? ', 's');
if strcmpi(strtrim(h), 'square')
fprintf('SQUARE!\n')
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 9 Okt. 2018
Try using contains():
clc;
userResponse = input('What shape do you want? ', 's')
if contains(userResponse, 'square', 'IgnoreCase', true)
uiwait(helpdlg('You want a square.'));
else
message = sprintf('%s is an unrecognized response.\nTry again.', userResponse);
uiwait(warndlg(message));
end

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by