How to close the figure contain the uifigure after the alert is closed?

91 views (last 30 days)
I'm not sure if this only happens to me.
Anyways, I create a simple alert using uifigure:
f = uifigure;
mess = "It seems like you have not loaded the model. Please do so before process further.";
title = "Error 101";
uialert(f, mess, title);
After pressing OK, the alert would go away, but the figure that contains the alert is still there (and I have to manually close it).
Is there anyway that it would automatically be closed after the user pressing ok? It's so annoying.
figure1.JPG
figure2.JPG
  2 Comments
Harryboy
Harryboy on 26 Nov 2020
Just a quick follow up, I have a similar question but the proposed solution is not working for my situation. My code is the following.
fig = uifigure;
title = 'Admin Mode';
msg = 'Do you want to choose the admin mode of operation?';
global optionChosen;
selection = uiconfirm(fig,msg,title,... % uiconfirm
'Options',{'Continue','Cancel'},...
'DefaultOption',1,'CancelOption',2,...
'Icon','question',...
'CloseFcn',@mycallback);
function [optionChosen] = mycallback(src,event)
global optionChosen;
optionChosen = event.SelectedOption;
end
In my case I am using 'uiconfirm' instead of 'uialert' which the OP uses.
and ...
I did try the following @(h,e) close(fig) after the 'closeFcn',@myCallback
selection = uiconfirm(fig,msg,title,... % uiconfirm
'Options',{'Continue','Cancel'},...
'DefaultOption',1,'CancelOption',2,...
'Icon','question',...
'CloseFcn',@mycallback,@(h,e)close(fig));
but this does modification is preventing the options to show up in the first place.
Rik
Rik on 26 Nov 2020
You did something strange when defining the CloseFcn. That should be a char array, a cell array, or a function handle. You separated two handles with a comma, which makes the second handle a new input. The code below works as expected.
fig=uifigure;
msg='foo';
title='bar';
selection = uiconfirm(fig,msg,title,...
'Options',{'Continue','Cancel'},...
'DefaultOption',1,'CancelOption',2,...
'Icon','question',...
'CloseFcn',@(h,e)mycallback(fig));
function mycallback(fig)
close(fig)
end

Sign in to comment.

Accepted Answer

Rik
Rik on 5 Feb 2020
Edited: Rik on 5 Feb 2020
The figure you specify does not actually contain the alert. The function only requires an input to determine where the alert box should be displayed. The code below makes sure to close the figure after the alert box is closed.
f = uifigure;
mess = "It seems like you have not loaded the model. Please do so before process further.";
title = "Error 101";
uialert(f, mess, title,'CloseFcn',@(h,e) close(f));
  2 Comments
Anh Tran
Anh Tran on 5 Feb 2020
Hey man, nice answer!
One more question, with this command:
uialert(f, mess, title,'CloseFcn',@(h,e) close(f));
What does @(h,e) supposed to be? I understand that that's the function handle but I have no idea what is the two variable h and e are.
Rik
Rik on 5 Feb 2020
In general all callbacks in Matlab have two inputs: the handle to the object causing the callback, and an event object (or struct). In this case we're ignoring both and just providing them to prevent an error (and because @(varargin) is longer).

Sign in to comment.

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!