4

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
rinkert
  • 6,593
  • 2
  • 12
  • 31
  • I figure out that if you don't call `registerApp(app, app.UIFigure)`, then `clear tG` closes the figure. `registerApp` holds a reference to the object, that prevents deletion. – Rotem Aug 17 '19 at 09:21
  • @Rotem hmm that did the trick in this MCVE, but not in my real GUI. It's a starting point, I will see what functionalities I can add/remove to make it reproducible. Any idea what changes this behavior? – rinkert Aug 18 '19 at 13:28
  • @Rotem adding a callback to the button changes the behavior, and the GUI stays open when the handle is deleted. Any other solution? – rinkert Aug 18 '19 at 14:33
  • I didn't mean removing `registerApp(app, app.UIFigure)` is a solution. Based on the following post: [MATLAB - run object destructor when using 'clear'?](https://stackoverflow.com/questions/7236649/matlab-run-object-destructor-when-using-clear), I assumed *"there may be other references to the object lingering"*... The added callback makes it weirder. – Rotem Aug 18 '19 at 16:49
  • I found a solution by overloading (overriding) `clear` method, based on the following [post](https://stackoverflow.com/questions/55745035/equivalent-of-evalin-that-doesnt-require-an-output-argument-internally). There are many issues overriding `clear`, and I don't think it's a good idea... Do you want me to post it anyway? – Rotem Aug 22 '19 at 19:03
  • @Rotem if `clear` can be overloaded only within the scope of this class that would be fine, globally overloading the function, no. Thanks for your effort though! – rinkert Aug 22 '19 at 19:13

0 Answers0