How do I user error check letters except "yes" or "no"?

조회 수: 4 (최근 30일)
Karl
Karl 2022년 11월 27일
답변: Walter Roberson 2022년 11월 27일
reset = 'Yes';
while strcmpi(reset, 'Yes') == 1
rreset = '1'; % reset condition to convert from string
reset = input('Do you want to run this program again? (Choose Yes or No) ','s');
while isempty(reset) == 1 || str2double(reset) >= str2double(rreset) || str2double(reset) <= str2double(rreset)
reset = input('Error, incorrect input. Please only choose between Yes or No ','s');
rreset = '1'; % put the reset condition back in the loop
end
end
Hello all, I'm fairly new to learning about strings. Here is some of my code i am having trouble at the end, I want to give the user an option to reset the program to the top just with "yes" or "no". I got it to error check numbers, however when I put any letters it just stops the program. How can I error check everything except "yes" or "no" ?

채택된 답변

Image Analyst
Image Analyst 2022년 11월 27일
Try this:
buttonText = 'Yes';
titleBarCaption = 'Continue?';
promptMessage = sprintf('Do you want to run the program again?');
loopCounter = 0;
maxIterations = 10; % Failsafe to prevent asking forever and ever.
while strcmpi(buttonText, 'Yes') && loopCounter < maxIterations
loopCounter = loopCounter + 1; % Increment iteration counter (this is our failsafe)
% Code to run some program.......
% Program is done running. Now ask if they want to run it again.
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if contains(buttonText, 'No', 'IgnoreCase', true)
break; % or return.
end
end
I don't know what you do to run the program. Maybe you just need to put the name of a script as one line of code, or maybe you need to make up a command line string and call system - I have no idea how you run your program

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 11월 27일
Use questdlg or listdlg . You get to specify the exact responses possible. The only response that the user can give that is not one on the list, is if the user asks to cancel the operation.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by