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.
2 个评论
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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!