how to close an UIFigure from a second app
14 次查看(过去 30 天)
显示 更早的评论
Hello !
Using app designer, I've created an application (app1) which, when I press a button (button1), opens a second application (app2). In app1, I also have a second button (button2) which, when pressed, opens a third app (app3). In app3, I have a button which, when pressed, executes a code and when it's finished, I'd like the code to close not only app3 but also app2. I've managed to close app3, but my problem is with closing app2.
In app1 and app3, I declared the app2 handle (app2handle) as a public property. However, in app3, when I use the line:
delete(app.app2handle);
app2 doesn't close.
The same line of code in app1, closes app2.
How can I do this?
0 个评论
采纳的回答
Ruchika Parag
2024-7-6
Hi GMabilleau, to close app2 from app3, you need to ensure that the handle to app2 is correctly passed and accessible within app3. Here is an example explaining how to do this :
In app1, declare a public property to store the handle of app2. When you create app3 from app1, pass the handle of app2 to app3:
classdef app1 < matlab.apps.AppBase
properties (Access = public)
UIFigure matlab.ui.Figure
Button1 matlab.ui.control.Button
Button2 matlab.ui.control.Button
app2handle
end
methods (Access = private)
% Button pushed function: Button1
function Button1Pushed(app, event)
app.app2handle = app2;
end
% Button pushed function: Button2
function Button2Pushed(app, event)
app3handle = app3;
app3handle.app2handle = app.app2handle;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 400 300];
app.UIFigure.Name = 'UI Figure';
% Create Button1
app.Button1 = uibutton(app.UIFigure, 'push');
app.Button1.Position = [100 200 100 22];
app.Button1.Text = 'Open App2';
app.Button1.ButtonPushedFcn = createCallbackFcn(app, @Button1Pushed, true);
% Create Button2
app.Button2 = uibutton(app.UIFigure, 'push');
app.Button2.Position = [100 150 100 22];
app.Button2.Text = 'Open App3';
app.Button2.ButtonPushedFcn = createCallbackFcn(app, @Button2Pushed, true);
end
end
% App initialization and construction
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
end
end
end
In the button callback of app3, where you want to close both app3 and app2, use the handle to app2 that was passed from app1:
classdef app3 < matlab.apps.AppBase
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
app2handle
end
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
% Your code execution here
% Close app2
if isvalid(app.app2handle) % Check if app2 is still valid
delete(app.app2handle);
end
% Close app3
delete(app);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 400 300];
app.UIFigure.Name = 'UI Figure';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.Position = [150 150 100 22];
app.Button.Text = 'Execute and Close';
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
end
end
% App initialization and construction
methods (Access = public)
% Construct app
function app = app3
% Create and configure components
createComponents(app)
end
end
end
Hope this helps!
更多回答(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!