User input with 3 options, how to repeat question if one of the options was not entered.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sean Cragg
el 24 de Jun. de 2017
Respondida: Image Analyst
el 24 de Jun. de 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 comentarios
Respuesta aceptada
Geoff Hayes
el 24 de Jun. de 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 comentarios
Más respuestas (1)
Image Analyst
el 24 de Jun. de 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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!