用MATLAB删除for循环中的一行

用MATLAB删除for循环中的一行,matlab,matrix,rotation,line,Matlab,Matrix,Rotation,Line,我可能在为这件事挣扎 我只是从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;

我可能在为这件事挣扎 我只是从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
我应该使用删除功能来获得动画而不打印吗 所有线路,但只有当前线路?!?。
提前感谢您的帮助和支持。

为此,您可以使用MATLAB中的
drawnow
函数。此函数可在
for
循环中使用,以强制每次迭代都渲染绘图,而不是在循环结束之前存储在缓冲区中

更新figure窗口并执行挂起的回调

一个例子:对于一个简单的代码,考虑对象在圆圈上的移动< /P>

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
请注意此方法中的
功能。如果从代码中删除
行,则在每次迭代中,轴限制将更改,动画将不平滑

您的代码是什么:

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功能保存(如果需要)此动画,并使用movie功能播放它

  • 另一个可能帮助您的功能是comet