MATLAB: Using uialert with uiwait and uiresume

MATLABuialertuiresumeuiwait

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

Best Answer

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.