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

90 次查看(过去 30 天)
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 个评论
Harryboy
Harryboy 2020-11-26
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 2020-11-26
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

请先登录,再进行评论。

采纳的回答

Rik
Rik 2020-2-5
编辑:Rik 2020-2-5
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 个评论
Anh Tran
Anh Tran 2020-2-5
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 2020-2-5
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).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by