2017-01-05 116 views
0

我與這件事可能 我剛開始用Matlab.this掙扎是我的代碼,我想 旋轉兩個環節的手臂,用旋轉矩陣:刪除一行在for循環與MATLAB

clear all 

close all 

clc 

myaxes = axes('Xlim',[-1 1 ],'Ylim',[-1 1],'Zlim',[-1 1]); 
view(3); 

grid on; 

axis equal; 

hold on 

xlabel('X') 

ylabel('y') 

zlabel('Z') 


P_0 = [0; 0; 0] ; 

P_1 = [-0.5;0; 0] ; 

P_2 = [-1; 0; 0] ; 

alfa_1 = 0:1:30 ; 

alfa_2 =(0:0.5:15) ; 

for i = 1:length(alfa_1) 

    M3(:,:,i) = [cosd(alfa_1(i)) -sind(alfa_1(i)) 0 
        sind(alfa_1(i)) cosd(alfa_1(i)) 0 
        0    0    1] ; 

    P_1(:,i,i) = M3(:,:,i)*[-0.5;0; 0] ; 
    P_2(:,i,i) = M3(:,:,i)*[-1;0; 0]; 
    figure(1) 
    line([0 P_1(1,i,i)],[0 P_1(2,i,i)],[0 P_1(3,i,i)]); 
    scatter(P_1(1,i,i),P_1(2,i,i)); 

    hold on 

    M3_1(:,:,i) = [cosd(alfa_2(i)) -sind(alfa_2(i)) 0 
        sind(alfa_2(i)) cosd(alfa_2(i)) 0 
        0    0    1] ; 
     P_2_2(:,i,i) = M3_1(:,:,i)*P_2(:,i,i); 
     line([P_1(1,i,i) P_2_2(1,i,i)],[P_1(2,i,i) P_2_2(2,i,i)],[P_1(3,i,i) P_2_2(3,i,i)],'color','r'); 
     scatter(P_2_2(1,i,i), P_2_2(2,i,i)) 
     hold on 
end 

我應該使用刪除功能來獲取動畫,而不是繪製所有行,但只繪製當前行?!?。 提前感謝您的幫助和支持。

回答

1

您可以在MATLAB中使用drawnow函數來達到此目的。該函數可以在for循環內部使用,以強制每個迭代繪製一個繪圖,而不是直到循環之後才存儲在緩衝區中。

更新圖窗口,執行掛起回調

一個例子:一個簡單的代碼考慮一圈對象移動

t = 0:0.05:2*pi; 
x = cos(t); 
y = sin(t); 

for k=1:length(t) 
    plot(x(k),y(k),'ko') 
    axis([-1.2 1.2 -1.2 1.2]) 
    axis square 
    drawnow 
end 

請注意,這種方法的axis功能。如果從代碼中刪除axis行,那麼在每次迭代中,軸限制將改變並且動畫不平滑。

你的代碼是什麼:

clear all 
close all 
clc 

view(3); 

xlabel('X'); 
ylabel('y'); 
zlabel('Z'); 

P_0 = [0; 0; 0] ; 
P_1 = [-0.5;0; 0] ; 
P_2 = [-1; 0; 0] ; 

alfa_1 = 0:1:30 ; 
alfa_2 = (0:0.5:15) ; 

for i = 1:length(alfa_1) 

    % Calculate new values for plotting 
    M3(:,:,i) = [cosd(alfa_1(i)), -sind(alfa_1(i)), 0 
        sind(alfa_1(i)), cosd(alfa_1(i)), 0 
        0    , 0    , 1] ; 

    P_1(:,i,i) = M3(:,:,i)*[-0.5; 0; 0] ; 
    P_2(:,i,i) = M3(:,:,i)*[-1; 0; 0] ; 

    % Clear figure 1 and hold for all plots 
    figure(1) 
    clf 

    % Hold only needs to be applied around plots on same axes 
    hold on 

    line([0 P_1(1,i,i)],[0 P_1(2,i,i)],[0 P_1(3,i,i)]); 
    scatter(P_1(1,i,i),P_1(2,i,i)); 

    % Recalculate plotting values  
    M3_1(:,:,i) = [cosd(alfa_2(i)), -sind(alfa_2(i)), 0 
        sind(alfa_2(i)), cosd(alfa_2(i)), 0 
        0    , 0    , 1] ; 

    P_2_2(:,i,i) = M3_1(:,:,i)*P_2(:,i,i); 

    line([P_1(1,i,i) P_2_2(1,i,i)], [P_1(2,i,i) P_2_2(2,i,i)], [P_1(3,i,i)    P_2_2(3,i,i)], 'color', 'r'); 
    scatter(P_2_2(1,i,i), P_2_2(2,i,i)) 

    % Set axis limits for consistency in all plots, show grid 
    axis([-2 2 -2 2]) 
    grid on  

    % Hold off is good practice to avoid later accidental plotting on same axes 
    hold off  

    % Draw from the buffer 
    drawnow 

    end 
  • 你可以節省(如果你想)這個動畫與的getFrame功能,並與電影功能

  • 和其他功能發揮它可能會幫助你彗星