Say I have a GUI class that creates an uifigure
(see below). I can create an instance of this class in my workspace:
tG = testGUI('hi!');
I can close the GUI by calling the delete
method: tG.delete()
. Is it also possible to automatically close the GUI when the handle tG
is cleared from the workspace by e.g.
clear tG
This would prevent opening many instances of the class when running some script multiple times, while the handle to the GUI is already deleted.
Update
- removing the
registerApp(app, app.UIFigure)
call in the constructor first seemed to solve the issue, however this did not work in my real GUI. - Adding a callback to the button in the test GUI reproduces the issue in my MVCE.
classdef testGUI < matlab.apps.AppBase
properties (Access = public)
% The figure handle used.
UIFigure
% app name
name
end
properties (Access = private)
pushButton
end
methods (Access = public)
function app = testGUI(name)
%TESTGUI - Constructor for the testGUI class.
% property management
app.name = name;
% create GUI components
createComponents(app)
% Register the app with App Designer
%registerApp(app, app.UIFigure); % removing this does not solve the issue
end
function delete(app)
delete(app.UIFigure)
end
end
methods (Access = private)
function createComponents(app)
%Create the components of the GUI
app.UIFigure = uifigure('Name', app.name);
app.UIFigure.Visible = 'on';
% some button
app.pushButton = uibutton(app.UIFigure, 'push');
app.pushButton.Text = 'This is a button';
app.pushButton.ButtonPushedFcn = @(src,event) someCallBack(app);
end
function someCallBack(app)
fprintf('this is someCallBack\n')
end
end
end