如何在MATLAB中绘制动画图

时间:2014-12-11 09:44:10

标签: matlab animation figure

我想在matlab图上绘制一个移动点,但我需要在绘制点的新位置时移动图形。为了更清楚,我想达到这个效果( Moving Vehicle)。

我发现了相关问题HereHere,但他们没有解决我的问题。

2 个答案:

答案 0 :(得分:0)

感谢来自@ Hoki的提示,我解决了这个问题。

ffn数据点(x,y)的数组,其大小(n * 2)。因为我的点在步骤0.001的窄范围内,所以我使用dxdy这些特定值。这取决于点步骤。

clf(figure(3)), hold on
dx = 0.001;
dy = 0.001;
for i = 1 : length(ff)-1
    minx = ff(i,1)-dx;
    miny = ff(i,2)-dy;
    maxx = ff(i,1)+dx;
    maxy = ff(i,2)+dy;
    plot(ff(i,1),ff(i,2),'o');
    axis([minx maxx miny maxy]);
    hold on
    pause(0.001)
end
hold off

答案 1 :(得分:0)

使用fifo缓冲区释放旧点并添加新点。功能' drawow'比暂停'

更快
close all
clear

NUM_OF_POINTS=100; % number of points displayed on plot
f=@(x) sin(2*pi*x); % function to plot
x_t=zeros(1,NUM_OF_POINTS); %x buffer
y_t=zeros(1,NUM_OF_POINTS); %y buffer

figure
keypressed=[];
set(gcf,'keypress','keypressed=get(gcf,''currentchar'');');
counter=0;
for t = 0:0.01:100
     counter=counter+1;
     %% Handle keyboard keys
     if ~isempty(keypressed)
          if(abs(keypressed)==27), break; end; %escape key. end
          if strcmp(keypressed,' '), keyboard; end; %spacebar key. pause
          keypressed=[];
     end

     %% Fill the buffer
     x_t=[x_t(2:end), t]; %fifo buffer
     y_t=[y_t(2:end), f(t)]; %fifo buffer

     %% Plot the buffer
     plot(x_t,y_t)
     drawnow; % this is the fastest for 'draw and continue'
     pause(0.01); %you can add delay to slow the move effect
end