Why does MATLAB not wait for me to click OK in a modal MSGBOX before moving on to the next command in the MATLAB file?

166 views (last 30 days)
I have the following sequence of commands in a MATLAB function:
disp(1:10);
msgbox('Draw a MESH plot?', 'modal');
contourf(peaks);
The MSGBOX comes up fine, but MATLAB also goes ahead and draws the CONTOURF plot without waiting for the me to click OK.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Jun 2021
Edited: MathWorks Support Team on 22 Jun 2021
Modal is not the same thing as "blocking execution". A modal dialog box prevents the user from interacting with other windows before responding, but does not prevent the remaining code in the MATLAB function from executing. MSGBOX is intended to put up a window containing information, and the "OK" button signifies that a person has seen the information. However, the dialog box does not suspend execution of a MATLAB function until user input.
To have a MATLAB function wait for something to happen before continuing to execute, you can either use a different type of dialog box like QUESTDLG, which is blocking, or you can use the WAITFOR command in conjunction with MSGBOX, which causes MATLAB to block execution until the graphics object is closed.
For example:
waitfor(msgbox('Wait for me.'));
disp('Done waiting.');
will display 'Done waiting' only after the OK button in the MSGBOX has been clicked.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!