matlab:用GUI输入生成函数输出变量

时间:2016-01-02 03:59:11

标签: matlab user-interface scope output nested-function

我维护的软件包的一个功能是将用户的产品速记名称解析为其全名。在踢回用户输入之前,软件会尝试两种方法自动执行此操作。如果自动尝试失败,则请求用户与表匹配。

GUI部分非常大,因此它保留在自己的子功能中。子函数只返回idx或索引变量。我很难让matlab“耐心”等待GUI指定idx。

以下是带注释的代码的重要部分:

function [ idx ] = mnmhelper( modeldb )
%mnmhelper makes a UI table for the user to manually select the correct
%model

%% uitable generation

f = figure('UserData',1); %userdata will be the selection;
t = uitable('Parent',f,...
    'Data',modeldb.Model,...
    'CellSelectionCallback',@select_callback);
b = uicontrol('Parent', f,...
    'Style','pushbutton',...
    'String','Commit Model Name',...
    'Callback',@button_callback);

%% callbacks - note that these are nested in the parent fnc

    function select_callback(hObject , eventdata)
        %hObject - handle to uitable
        %eventdata - currently selected table indexes

        f.UserData = eventdata.Indices; % pass selection as userdata array: [row,col]
    end

    function button_callback(hObject,eventdata,selection)
        idx = f.UserData(1);
        close(f);
        figclosed = 1; %see additional notes below code on this line
    end
end

问题是matlab会错误地定义idx,因为它没有等待使用该数字。

我试图添加以下部分:

%% strongarm matlab into waiting for user to do this

figclosed = 0;
while figclosed < 1 %don't evaluate to command line until figure is finished
    % ... do nothing 
    % once this evaluates to ==1 and kicks out of this, idx is defined
end

在所有回调之后,但是matlab将在while循环中等待并且数字不会生成。我如何让matlab等待?

我是否需要CreateFcn才能使matlab等待?

1 个答案:

答案 0 :(得分:0)

uiwait函数仅用于此目的。只需调用uiwait(f)让matlab等待GUI图终止。

请参阅doc:uiwait