matlabmatlab-gui

MATLAB Gui, text-box value on buttonpress without GUIDE


I have a certain GUI built without GUIDE, just plain old uicontrols and I've gotten everything to work properly so far. However I want to, upon a button press grab the value in a text-box (edit) and store it into a variable fi.

Basically the code in question;

c2 = uicontrol(f,'Style', 'pushbutton','String','Rotation','Callback', 
@rotation);
s1 = uicontrol(f,'Style', 'edit');

function rotation(src,event)
   load 'BatMan.mat' X
   fi = %This is the value I want to have the value as the edit box.
   subplot(2,2,1)
   PlotFigure(X)
end

Solution

  • Easiest is to get rotation to know about s1 through an input argument:

    c2 = uicontrol(f,'Style', 'pushbutton','String','Rotation');
    s1 = uicontrol(f,'Style', 'edit');
    set(c2,'Callback',@(src,event)rotation(s1,src,event));
    
    function rotation(s1,src,event)
       load 'BatMan.mat' X
       fi = get(s1,'String');
       subplot(2,2,1)
       PlotFigure(X)
    end
    

    Here, we set the callback for c2 to be an anonymous function with the right signature (2 input arguments), and which calls rotation with s1 as an additional argument. The callback now has the handle s1 embedded in it.