2011-12-21 52 views
0

以下代碼是向東旋轉的地球地球的圖形呈現。我有兩個按鈕,旋轉和停止。兩者共享相同的回調函數hAnimaCallback。特別是後者不起作用。我假裝使用按鈕的字符串(名稱)來創建一個停止移動的開關。然而,我不能改變在while循環內的字符串名稱,我不明白爲什麼我的方法是錯誤的。在回調中更新字符串

function example 
fh = figure('Menu','none','Toolbar','none','Units','characters'); 
T = 0:pi/100:2*pi; 
Y = zeros(numel(T),3); 
Y(:,1) = 7000*cos(T); 
Y(:,2) = 7000*sin(T); 
hPanAni = uipanel('parent',fh,'Units','characters','Position',... 
    [22.6 10.4 53 23],'title','Controls','FontSize',11,... 
    'FontAngle','italic','FontWeight','bold'); 
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',... 
    'Position',[0.14 0.64 0.5 0.12],'String','Spin',... 
    'FontSize',10,'Callback',@hAnimaCallback); 
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',... 
    'Position',[0.14 0.26 0.5 0.12],'String','Stop',... 
    'FontSize',10,'Callback',@hAnimaCallback); 
hPantSim = uipanel('Parent',fh,'Units','characters',... 
    'Position',[107.87 8 157.447 42],'BorderType','none','title',... 
    'Screen','FontSize',11,'FontAngle','italic',... 
    'FontWeight','bold'); 
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',... 
    [0 0 1 1],'BorderType','line','BackgroundColor','black'); 
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',... 
    [0 0 1 1],'Color','none','Visible','off','DataAspectRatio',... 
    [1 1 1],'NextPlot','add'); 
rotate3d(ah4); 
hgrot = hgtransform('Parent',ah4); 
Resf = 6378; 
maptext = imread('tierra.jpg'); 
[X, map] = rgb2ind(maptext,128); 
[x,y,z] = sphere(50); 
x = Resf*x; 
y = Resf*y; 
z = Resf*z; 
props.FaceColor= 'texture'; 
props.EdgeColor = 'none'; 
props.Parent = hgrot; 
props.Cdata = flipud(X); % it is necesary to do this for getting the 
% appropiate image on the sphere 
hsurf = surface(x,y,z,props); 
colormap(map); 
axis equal; 
view([71 14]); 
set(gcf,'Renderer','opengl') 
drawnow; 
line('parent',ah4,'XData',Y(:,1),'YData',Y(:,2),'ZData',... 
    Y(:,3),'Color','red','LineWidth',2); 
line('parent',ah4,'XData',Y(end,1),'YData',Y(end,2),... 
    'ZData',Y(end,3),'Marker','o','MarkerSize',6,'MarkerFaceColor','b'); 
axis square equal vis3d; 
view(3); 
handles.XLim = get(ah4,'XLim'); 
handles.YLim = get(ah4,'YLim'); 
handles.ZLim = get(ah4,'ZLim'); 
xmin = handles.XLim(1); 
ymin = handles.YLim(1); 
zmin = handles.ZLim(1); 
xmax = handles.XLim(2); 
ymax = handles.YLim(2); 
zmax = handles.ZLim(2); 
set(ah4, 'XLim', [xmin xmax],'YLim', [ymin ymax],'Zlim',[zmin zmax]); 
az = 0; 
    function hAnimaCallback(hObject,evt) 
     while (ishandle(fh)) 
      state = get(hObject,'String'), % state should change between the states of 
      % Spin and Stop but this does not occur 
      if (strcmp(state,'Stop')) 
       break; 
      else 
       az = az + 0.01745329252; 
       set(hgrot,'Matrix',makehgtform('zrotate',az)); 
       drawnow; 
      end 
     end 
    end  
end 

回答

1

好像你正在運行到某種因爲while循環反覆drawnow通話競爭條件。當您按下「停止」按鈕時,狀態將更改爲「停止」,但需要快速注意。基本上,while循環運行速度與MATLAB運行速度一樣快。取而代之的drawnow使用pause(0.1)命令似乎工作(有一點點的代碼邏輯的變化):

az = 0; 
spin = false; 
    function hAnimaCallback(hObject,~) 
    state = get(hObject,'String') 
    if strcmp(state, 'Spin') 
     spin = true; 
    else 
     spin = false; 
    end 

    while (spin) 
     az = az + 0.01745329252; 
     set(hgrot,'Matrix',makehgtform('zrotate',az)); 
     pause(0.1); 
    end 
    end 
+0

非常感謝您的回答。 – julian 2011-12-21 23:33:39