필터 지우기
필터 지우기

How to make a loop repeat itself when user typed the wrong letter?

조회 수: 2 (최근 30일)
S C
S C 2023년 3월 20일
댓글: S C 2023년 3월 20일
Hi all,
I created a loop where the user need to state whether the already stated filepath in the script is correct. If not they can select the desired filepath. I'm also trying to make the user type 'y' or 'n' and if they have typed anything else i want the loop to repeat itself unitil the correct letter is typed. But i am struggling to do the last part.
This is what the coding looks like right now:
p='E:\...';
fprintf('Data to be analysed is located in: \n');
fprintf('<strong>%s\n</strong>', p)
x=input('\nIs the file path correct? Y/N: ','s');
if strcmpi(x, 'Y');
disp('Thank you, the file path has been saved.')
...
elseif strcmpi(x, 'N');
disp('Please select the required file path.')
p=uigetdir(path);
fprintf('Thank you, the new file path has been saved as: \n');
fprintf('<strong>%s\n</strong>', p);
...
else strcmpi(x,'');
disp('Please type "Y" or "N"');
x=input('Is the file path correct? Y/N: ','s');
...
end
As you can see the last part, if the user type the wrong letter twice it will stop the loop but i want it to repeat until the correct letter is typed.
Any suggestion on how i can do so?
Thank you :)

채택된 답변

Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023년 3월 20일
Hello S C,
You should use while instead of for loop.
p='E:\...';
fprintf('Data to be analysed is located in: \n');
fprintf('<strong>%s\n</strong>', p)
x=input('\nIs the file path correct? Y/N: ','s');
while ~strcmpi(x, 'Y') && ~strcmpi(x, 'N') ; % It will only exit this loop when Y or N are pressed
disp('Please type "Y" or "N"');
x=input('Is the file path correct? Y/N: ','s');
end
if strcmpi(x, 'Y');
disp('Thank you, the file path has been saved.')
elseif strcmpi(x, 'N');
disp('Please select the required file path.')
p=uigetdir(path);
fprintf('Thank you, the new file path has been saved as: \n');
fprintf('<strong>%s\n</strong>', p);
end
Hope this helps!

추가 답변 (1개)

Abb
Abb 2023년 3월 20일
편집: Abb 2023년 3월 20일
You can achieve this by putting the code block inside a while loop and using a flag variable to control the loop. Here's an example:
p='E:\...';
fprintf('Data to be analysed is located in: \n');
fprintf('<strong>%s\n</strong>', p)
flag = false;
while ~flag
x=input('\nIs the file path correct? Y/N: ','s');
if strcmpi(x, 'Y');
disp('Thank you, the file path has been saved.')
flag = true;
elseif strcmpi(x, 'N');
disp('Please select the required file path.')
p=uigetdir(path);
fprintf('Thank you, the new file path has been saved as: \n');
fprintf('<strong>%s\n</strong>', p);
else
disp('Please type "Y" or "N"');
end
end
This will continue to prompt the user for input until they enter either "Y" or "N". The flag variable is initially set to false, and the loop will continue to run until it is set to true when the user enters "Y".
  댓글 수: 1
S C
S C 2023년 3월 20일
Hi, thank you. This worked as i wanted as well but i needed to add 'flag=true' at the end of the elseif statement (between line 15 and 16).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by