首页 > 解决方案 > 从工作区删除句柄时自动关闭 uifigure

问题描述

假设我有一个创建一个uifigure(见下文)的 GUI 类。我可以在我的工作区中创建这个类的一个实例:

tG = testGUI('hi!');

我可以通过调用方法关闭 GUI deletetG.delete(). tG当句柄从工作区中清除时,是否也可以自动关闭 GUI ,例如

clear tG

这将防止在多次运行某些脚本时打开该类的许多实例,而 GUI 的句柄已被删除。

更新


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

标签: matlabmatlab-figurematlab-app-designer

解决方案


推荐阅读