Hc_cur-findobj(gcf , ' Tag ' , ' AZcur ' ) ;
str = num2str(get(Hc_az , ' Value ' )) ;
newview - [get(Hc_az , ' Value ' ) vw(2)] ;
set(Hc_cur , ' String ' , str)
set(gca, ' View ' ,newview)
end
上述的两个例子均设置了 ' tag ' 属性,利用该属性和函数 findobj 寻找回调函数所需对象的句
柄。另外两种方法将在下章描述。
全局变量
全局变量可用在函数中,使某些变量对 GUI 函数的所有部分都可用,全局变量是在函数的
公共区说明,因此整个函数以及所有对函数的递归调用都可以利用全局变量,下面的例子说
明如何利用全局变量将方位角滑标编程。
function setview(switch)
global HC_AZ HC_CUR % Create global variables
if nargin < 1
,
switch = ' startup '
;
end
;
if ~isstr(switch , error( ' Invalid argument. ' ) ; end;
vw = get (gca , ' View ' ) ; % This information is needed in both sections
if strcmp(switch , ' startup ' ) % Define the controls
Hc_AZ=uicontrol(gcf , ' style ' , ' slider ' ,...
' Position ' ,[10 5 140 20] , ...
' Min ' ,-90, ' Max ' ,90, ' Value ' ,vw(1) ,...
' Callback ' , ' setview( ' set ' ) ' );
Hc_min=uicontrol(gcf , ' Style ' , ' text ' ,...
' Position ' ,[10 25 40 20] , ...
' String ' ,num2str(get(Hc-AZ , ' Min ' ,)));
HcMax=uicontrol(gcf , ' Style ' , ' text ' ,...
' Position ' ,[110 25 40 20] ,...
' String ' ,num2str(get(Hc_AZ , ' Max ' ))) ;
Hc_cur=uicontrol(gcf
,
' style '
,
' text '
,
...
' Position ' ,[60 25 40 20] , ...
' String '
,
num2str(get(HC_AZ
,
' Value ' )))
;
elseif strcmp(switch , ' set ' ) % Execute the Callback
str=num2str(get(HC_AZ , ' Value ' )) ;