Auto close uifigure upon deletion of handle from workspace

16 views (last 30 days)
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
I see my previous example did not really reflect what I am trying to achieve. The thing is that I need to keep a handle to the instance, to call a method to add data to the instance (and later visualize this data in the GUI, not shown below):
tg.add_data(rand(10),1);
So, when I am done with one script, in which I add data, manipulate this data from the GUI, and then save the data, I'd like to start a fresh instance.
I tried adding the following to the top of my script
try
close(lg)
catch ME
disp(ME)
end
clear
close all
% start stuff
lg = testGUI('Hi');
for k = 1:10
lg.add_data(rand(10), k);
end
But when the handle is accidentally cleared from my workspace, this does not work anymore, and again a new GUI is opened.
Example GUI:
classdef testGUI < matlab.apps.AppBase
properties (Access = public)
% The figure handle used.
UIFigure
% app name
name
% some data (matrix as a simple example)
data_matrix
end
properties (Access = private)
pushButton
end
methods (Access = public)
function app = testGUI(name)
%TESTGUI - Constructor for the testGUI class.
% property management
app.name = name;
app.data_matrix = NaN(10,10,5); % init the data matrix
% 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
function add_data(app, dataIn, n)
% add some data
app.data_matrix(:,:,n) = dataIn;
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

Answers (1)

Adam Danz
Adam Danz on 18 Aug 2019
Edited: Adam Danz on 18 Aug 2019
"This would prevent opening many instances of the class when running some script multiple times, while the handle to the GUI is already deleted."
A better solution would be to check if the GUI exists prior to creating a new instance of the GUI. That can be done with two simple additions to your code.
First, in the createComponents() function, assign a unique tag to your UI figure.
app.UIFigure = uifigure('Name', app.name);
app.UIFigure.Visible = 'on';
app.UIFigure.Tag = 'testGUI_tag'; % add some unique tag name to the figure
Second, in the testGUI(name) function, check if any figures exist with the matching tag.
% At the very beginning of the function...
% Detect if GUI already exists by searching for tag
h = findall(0,'type','figure','tag','testGUI_tag');
if ~isempty(h)
% Do no create new UI figure
return
end
  2 Comments
rickert
rickert on 18 Aug 2019
Thank you for your suggestion. This is however not the behavior I am looking for, I updated the question a bit, could you have another look?
Adam Danz
Adam Danz on 18 Aug 2019
Edited: Adam Danz on 19 Aug 2019
"But when the handle is accidentally cleared from my workspace, this does not work anymore, and again a new GUI is opened. "
If it were my project, the first thing I'd do is prevent the handle from being deleted in the first place. The clearing of variables should be intentional and controlled.
In any case, here's an alternative suggestion.
You can determine if the handle still exists and is a figure handle. If it does not exist or is not a figure handle, you can use a line from my answer to get the handle back (assuming the ui figure still exists). You'll also need to assign the tag to the gui as is demonstrated in my answer.
% If ui figure handles no longer exists or was overwritten, get the handle from the
% existing GUI
if exist('h','var') == 0 && ishghandle(h,'figure')
h = findall(0,'type','figure','tag','testGUI_tag');
end
This is a very common way to get the handle of a graphics object.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!