User input with 3 options, how to repeat question if one of the options was not entered.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sean Cragg
am 24 Jun. 2017
Beantwortet: Image Analyst
am 24 Jun. 2017
I am trying to add something to the end of a function that will be looped over and over to ask the user whether they are happy with the results, at which they can type 'yes', 'no', or 'trash'. If the user types something else the question should be repeated until one of these options is entered. I tried to use the code:
UI = 'aaa'
while UI ~= 'yes' & UI ~= 'no' & UI ~= 'trash' %ERROR AT THIS LINE
UI = input("Are you happy with this? Enter: 'yes' 'no'(to manually change) or 'trash' skip and move onto next unit.", 's')
end
if UI == 'yes'
% do something
return
elseif UI == 'no'
% do something
else UI == 'trash'
% do something else
end
I have tried the while loop with both '&' and '&&' and neither works, was wondering if anyone could help me out, thanks.
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 24 Jun. 2017
Error using ~=
Matrix dimensions must agree.
when comparing strings like how you are doing. Your code would then become
UI = [];
while ~strcmpi(UI,'yes') && ~strcmpi(UI,'no') && ~strcmpi(UI,'trash') %ERROR AT THIS LINE
UI = input('Are you happy with this? Enter: ''yes'' ''no''(to manually change) or ''trash'' skip and move onto next unit.', 's');
end
Note how single quotes are used (instead of double quotes) to wrap your string and that we use two single quotes on either side of (say) yes so that the single quotes appear in the output string.
Try the above and see what happens!
0 Kommentare
Weitere Antworten (1)
Image Analyst
am 24 Jun. 2017
Then don't do it that way. Use questdlg() like you're supposed to:
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return; % or break or continue - whatever is appropriate.
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!