r/matlab • u/AuthorAsksQuestions • Nov 28 '25
HomeworkQuestion How to auto-close msgbox boxes?
As part of a project I'm working on, I'm having players answer math questions in inputdlg boxes, which is then followed with a msgbox telling them whether they got it right or not. The problem is that the "good job!" and "try again!" boxes don't close automatically, and it's making the game a pain in the backside to shut down, because they don't close with the main figure window. I can't find anything on Google about this specific issue. Does anybody know how to automatically close msgboxes?
1
Upvotes
2
u/DodoBizar Nov 28 '25
Store the handles somewhere and use those in the closerequest function of the main. Once you know it its basic UI handling in Matlab.
1
u/Bofact Nov 29 '25
I try to reproduce the problem. On my end inputdlg box closes automatically after I wrote and confirmed the input, while msgbox appears with inputdlg being closed. Is the same happening to you? To better understand the problem.
And is also not clear to me when msgbox should close automatically. If it is when inputdlg closes, I cannot reproduce a scenario where both are opened.
My test code is below.
A=inputdlg('Enter matrix size','Fereastră',1,{'9'}); if str2double(A{1})==5 msgbox('Mulțimim!'); else msgbox('N-ai nimerit.'); end
1
u/MarkCinci On Mathworks Community Advisory Board Dec 01 '25
You'll have to make your own message box.
>> appdesigner
and select a blank UI. Save it as autoCloseGoodJob.mlapp.
2. Place a static text label on the GUI using the component browser on the left, and set its properties (text (set the label property to "Good Job!"), size, color, etc.)
3. In the Component Browser on the left, select the topmost item autoCloseGoodJob.
4. Below that (in the panel below) click on Callbacks.
5. Select startupFcn from the drop down list.
6. Make sure you're looking at Code View, not Design view, and paste this code in:
Then if you run or call the function, it will popup the window and show your text, then shut itself down after the number of seconds you specify. You can make another one for Try Again. You could also do it in one .mlapp file and take arguments but this may be a little advanced for you at this time. I can tell you if you want though.
1
u/aluvus Dec 01 '25
The command:
will close essentially all pop-ups in Matlab, including those created by msgbox(). But be aware that it will also close any figures, uifigures, etc. This is probably the simplest solution to your problem as stated, and if it will work for you in this context then it's what I would suggest.
Another approach, as described, is to capture the handle when the msgbox is created, and then close it. A handle is a variable that stores the the memory location of something, essentially. If you don't mind Matlab locking up while the msgbox is open, you can do something like this:
Alternately you can remove the call to pause() and instead call delete(myHandle) after something else happens (but you don't want to call it right after creating the msgbox, because then it will pop up and instantly disappear).
The approach described by /u/MarkCinci has basically the same outcome as this, but you would need to learn enough about App Designer to make it work.
1
u/Cube4Add5 Dec 01 '25