Close a message box in a while loop

조회 수: 7 (최근 30일)
Nev Pires
Nev Pires 2020년 8월 27일
댓글: Nev Pires 2020년 8월 27일
I have a while loop that contains a message box if the user doesn't define the input correctly. I would like to close the message box before the loop resets.
For example:
cityNames = {'London', 'Paris', 'Rome', 'Dubai'};
yes = false;
while yes == false
%% Define City Name
cityInput = inputdlg({'Enter City Name'});
city = cityInput{1};
if any(strcmp(city, cityNames))
yes = true;
else
msgbox('The City you entered is not in the list. Please enter a city in the list');
end
end
The code works perfectly if one of the cities in the cityNames cell is entered correctly. However, if a different city is entered, or one of the names is entered incorrectly, the msgbox displays as it should, however, before the user can press "ok", the prompt to "Enter City Name" appears before they have a chance to close the message box.
I'd like to be able to close the message box before the city input dialog box appears again.
Thanks in advance!

채택된 답변

Mehmed Saad
Mehmed Saad 2020년 8월 27일

Approach-1

f = msgbox('The City you entered is not in the list. Please enter a city in the list');
while(ishandle(f))
pause(0.1);
end
disp('msg box test')

Approach-2

Use questdlg for that purpose
questdlg('The City you entered is not in the list. Please enter a city in the list', ...
'','Ok','Ok');
disp('quest dlg test')
  댓글 수: 1
Nev Pires
Nev Pires 2020년 8월 27일
Approach 2 works perfectly! Thank you so much!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by