omid
omid

Reputation: 3

UIcontrol callback

I want make a main GUI with a Bushbutton(Bushbutton). when press pb1 => open a figure(Fig) with a pushbutton(Upb1), a Edit(Uet1) and a text (Ust1). I want: when press Upb1 , Ust1 show the number of Uet1 (also, when change Uet1 and press Upb1, Ust1 change).

I write this code but didn't work (var1 is undefined). What should I do?

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)

Upvotes: 0

Views: 2383

Answers (1)

angainor
angainor

Reputation: 11810

You need to change the scope of the second function so that it can access variable Uet1 and Ust1. That means, you need to implement it in the scope of Bushbutton_Callback (might want to call it 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

Upvotes: 1

Related Questions