UIcontrol回调

时间:2012-09-27 10:31:38

标签: matlab user-interface matlab-guide

我想用Bushbutton(Bushbutton)制作一个主GUI。 当按pb1 =>用按钮(Upb1),编辑(Uet1)和文本(Ust1)打开图形(图)。 我想:当按Upb1时,Ust1显示Uet1的数量(同样,当改变Uet1并按Upb1,Ust1改变时)。

我编写此代码但不起作用(var1未定义)。我该怎么办?

function Bushbutton_Callback(hObject, eventdata, handles)       % main
      Fig=figure('MenuBar','none');
      Ust1= uicontrol(Fig,...
               'Position',[50 60 80 20 ],...
               'Style','text')
      Uet1= uicontrol(Fig,...
              'Position',[50 90 80 20 ],...
              'Style','edit',...
              'string',10,...
              'callback',@printNum)
     Upb1= uicontrol(Fig,...
              'Position',[50 30 80 20 ],...
              'Style','pushbutton',...
              'callback',@printNum)


function printNum(hObject, eventdata,handles)         

    var1=get(Uet1,'string')
    set(Ust,'string',var1)

1 个答案:

答案 0 :(得分:1)

您需要更改第二个函数的范围,以便它可以访问变量Uet1和Ust1。这意味着,你需要在Bushbutton_Callback的范围内实现它(可能想称之为Pushbutton_Callback ......)

function Bushbutton_Callback(hObject, eventdata, handles)
      Fig=figure('MenuBar','none');
      Ust1= uicontrol(Fig,...
           'Position',[50 60 80 20 ],...
           'Style','text')
      Uet1= uicontrol(Fig,...
          'Position',[50 90 80 20 ],...
          'Style','edit',...
          'string',10,...
          'callback',@printNum)
     Upb1= uicontrol(Fig,...
          'Position',[50 30 80 20 ],...
          'Style','pushbutton',...
          'callback',@printNum)

    function printNum(hObject, eventdata,handles)         
        var1=get(Uet1,'string')
        set(Ust1,'string',var1)
    end

end