How to close the figure contain the uifigure after the alert is closed?
조회 수: 84 (최근 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!