2013-03-17 76 views
2

我一直在试图编写一个应该接收一个变量作为输入的GUI,并执行几个生成另一个变量的操作。 GUI将有一个关闭GUI的按钮。MATLAB:从编程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

回答

2

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

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 
+0

非常感谢@jucestain 在一个侧面说明,是变量'outputVariable'和'newVariable'认为** **全局变量?我一直在阅读,避免全局变量是一种很好的做法,但我似乎无法理解我们如何避免使用嵌套函数在程序化GUI中使用它们......对此有何看法? (假设我所指的变量确实是全局变量) – fnery 2013-03-17 21:45:00

+0

不,它们不是全局的。它们的范围仅限于整个功能。这意味着'exampleGUI()'中的任何回调都可以访问它们。一旦函数返回,它们就会被销毁。另外,不要忘记接受答案:) – Justin 2013-03-17 21:51:05

+0

嘿!答案接受:) 再次感谢。 – fnery 2013-03-17 22:21:57