MATLAB Gui,在没有 GUIDE 的情况下按下按钮时的文本框值

MATLAB Gui, text-box value on buttonpress without GUIDE

我有一个没有使用 GUIDE 构建的特定 GUI,只有普通的旧 uicontrols,到目前为止我已经让一切正常工作。但是我想在按下按钮时获取文本框中的值(编辑)并将其存储到变量 fi 中。

基本上是有问题的代码;

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

最简单的方法是通过输入参数让 rotation 了解 s1

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

在这里,我们将 c2 的回调设置为具有正确签名(2 个输入参数)的匿名函数,并使用 s1 作为附加参数调用 rotation .回调现在嵌入了句柄 s1