how to close an UIFigure from a second app
조회 수: 12 (최근 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!