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?

144 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 22 Jun. 2021
Bearbeitet: MathWorks Support Team am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by