【问题标题】:MATLAB: Returning variables from programmatic GUIsMATLAB:从编程 GUI 返回变量
【发布时间】:2013-03-06 02:44:11
【问题描述】:

我一直在尝试编写一个 GUI,它应该接收一个变量作为输入并执行几个生成另一个变量的操作。 GUI 将有一个关闭 GUI 的按钮。

我不(也不想)使用 GUIDE。

下面,我提供了一个简单的 GUI 工作示例,它只是将一个添加到输入变量。 “完成”按钮关闭 GUI,但 我找不到将变量导出到工作区的方法

% Is this the correct way to initialize the function for what I am trying to do?
function outputVariable = exampleGUI(inputVariable) 

    % Initialize main figure
    hdl.mainfig = figure();

    % Add Button
    hdl.addPushButton = uicontrol(hdl.mainfig, 'Units', 'normalized',
                                  'Position', [0.05 0.6 0.3 0.25], 'String',
                                  'Add One', 'Callback', @addOne);
    % Done Button
    hdl.donePushButton = uicontrol(hdl.mainfig, 'Units', 'normalized',
                                   'Position', [0.65 0.6 0.3 0.25], 'String',
                                   'Done', 'Callback', @done);
    % Static text
    hdl.sliceNoText = uicontrol(hdl.mainfig, 'Style', 'text',
                                'Fontsize', 16, 'Units', 'normalized', 
                                'Position', [0.35 0.2 0.3 0.25]);

    function addOne(~, ~, ~)
        inputVariable = inputVariable + 1; % add one to the current inputVariable
        set(hdl.sliceNoText, 'String', num2str(inputVariable)); % change static text
        newVariable = inputVariable; % new variable to be exported
    end

    function done(~, ~, ~)   
        delete(hdl.mainfig); % close GUI
    end

end

我想做这样的事情:

在工作区:

outputVariable = exampleGUI(inputVariable)

在向输入变量添加一定次数后,我将按下“完成”按钮,GUI 将关闭,工作区将包含 inputVariable 和 outputVariable。

非常感谢。

fnery

【问题讨论】:

    标签: matlab user-interface variables


    【解决方案1】:

    这是您可以做的一个示例。您可以做很多事情来使用您想要的功能。通常,除了输入和输出以及 guihandles 之外,我不喜欢在整个函数的工作区中有任何变量。我使用setappdatagetappdata 来存储其他变量并让它们可以被回调访问。这取决于您,但以下是如何使您的简单 gui 工作的示例。 CloseRequestFcn 允许您处理如果用户只是关闭 gui 会发生什么。希望这可以帮助。此外,waitfor 阻止函数返回,直到函数关闭。如果需要,您还可以将图形的'WindowStyle' 属性设置为'modal',以强制用户在关闭图形界面之前输入。

    function outputVariable = exampleGUI(inputVariable) 
    
        % Any variables declared here will be accessible to the callbacks
        % Initialize output
        outputVariable = [];
    
        % Initialize newVariable
        newVariable = [];
    
        % Initialize main figure
        hdl.mainfig = figure('CloseRequestFcn',@closefunction);
    
        % Add Button
        hdl.addPushButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.6 0.3 0.25], 'String', 'Add One', 'Callback', @addOne);
        % Done Button
        hdl.donePushButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.65 0.6 0.3 0.25], 'String', 'Done', 'Callback', @done);
        % Static text
        hdl.sliceNoText = uicontrol(hdl.mainfig,'Style','text','Fontsize',16,'Units','normalized','Position',[0.35 0.2 0.3 0.25]);
    
    
        function addOne(hObject,eventdata) 
            inputVariable = inputVariable+1; % add one to the current inputVariable
            set(hdl.sliceNoText, 'String', num2str(inputVariable)); % change static text
            newVariable = inputVariable; % new variable to be exported
        end
    
        function closefunction(hObject,eventdata) 
            % This callback is executed if the user closes the gui
            % Assign Output
            outputVariable = newVariable;
            % Close figure
            delete(hdl.mainfig); % close GUI
        end
    
        function done(hObject,eventdata)  
            % Assign Output
            outputVariable = newVariable;
            % Close figure
            delete(hdl.mainfig); % close GUI
        end
    
        % Pause until figure is closed ---------------------------------------%
        waitfor(hdl.mainfig);    
    end
    

    【讨论】:

    • 非常感谢@jucestain 附带说明一下,变量outputVariablenewVariable 是否被视为全局 变量?我一直在读到避免全局变量是一种很好的做法,但我似乎无法理解我们如何才能避免在使用嵌套函数的编程 GUI 中使用它们……对此有什么想法吗? (假设我指的变量确实是全局变量)
    • 不,它们不是全局的。它们的范围刚好涵盖整个功能。这意味着exampleGUI() 中的任何回调都可以访问它们。一旦函数返回,它们就会被销毁。另外,不要忘记接受答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多