Using uialert with uiwait and uiresume

48 views (last 30 days)
Hi all, I have a GUI where asks the user to save a file. I check the filename and depending on the result either carry on or ask the user to change it. All of this is contained within a while loop so that it will continually loop around until the checks have passed or the user has cancelled.
Within this loop I have the alert that pops up stating the error
uialert(app.mainGuiUIFigure,{'New file name must not be the same as the default name, choose another'},'File name error','Icon','warning');
I want this to block execution of the code until the user presses ok. So I have tried using uiwait
uialert(app.mainGuiUIFigure,{'New file name must not be the same as the default name, choose another'},'File name error','Icon','warning','CloseFcn','uiresume');
uiwait
but it creates a blank figure window first. It will stop execution and will only resume once I close the alert window, but this new figure has popped up. How do I prevent this figure window from popping up? Is there a better way to do what I want?
I have successfully used waitfor(warndlg(...)); before, but as this is in appdesigner, I thought I'd use the new dialogue boxes to match the newer GUI graphics stlye.
Matlab 2017b, Windows 7-64bit

Accepted Answer

Alexander Kornegay
Alexander Kornegay on 26 Jul 2019
Try this instead:
uialert(app.mainGuiUIFigure,{'New file name must not be the same as the default name, choose another'},'File name error','Icon','warning','CloseFcn','uiresume(gcbf)');
uiwait(gcbf)
This will use the current figure instead of creating a new one in the process.
  5 Comments
Adam Gogacz
Adam Gogacz on 3 Oct 2020
Edited: Adam Gogacz on 3 Oct 2020
Actually, "uiconfirm" with the "Option" set to "OK" will do. Make sure you are assigning the call to "uiconfirm" to a dummy variable to pause the execution.
David Young
David Young on 1 Mar 2021
Thank you Adam Gogacz, both for the analysis and the suggestion to use uiconfirm. But what a shame that uialert doesn't return an object to wait for - that greatly limits the situations where it's useful.

Sign in to comment.

More Answers (3)

lihiniya
lihiniya on 21 Jan 2022
You can use uiconfirm instead of uialert.
answr=uiconfirm(app.AiDAAnalysisV201alphaUIFigure,...
"New file name must not be the same as the default name, Choose another",...
"Warning",'Options',"OK",'Icon','warning');
You don't need to do anything with returned selection, because there's one possible option "OK".

Ron Fredericks
Ron Fredericks on 26 Apr 2022
Edited: Ron Fredericks on 26 Apr 2022
I thank that Vas almost had the correct answer with:
uialert(app.mainGuiUIFigure,{'New file name must not be the same as the default name, choose another'},'File name error','Icon','warning','CloseFcn','uiresume');
Try this instead, it works for me when I need to wait for a response then close out the window (Note the @ sign). Also no uiwait required:
uialert(app.mainGuiUIFigure,{'New file name must not be the same as the default name, choose another'},'File name error','Icon','warning','CloseFcn',@(h,e) close(app.mainGuiUIFigure));
More details here:
https://www.mathworks.com/matlabcentral/answers/503729-how-to-close-the-figure-contain-the-uifigure-after-the-alert-is-closed#answer_413884

Marco Avalos
Marco Avalos on 8 Mar 2024
Edited: Marco Avalos on 8 Mar 2024
I use a line creating a variable which is the uifigure that I am using for the app, and then use UIconfirm and that did the job. these are the lines I used
fig = app.figure1; % In app designer this is the main UIfigure for your GUI
uiconfirm(fig,'Please select one option','Selection error');

Categories

Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!