【问题标题】:How to Draw animated Figure in MATLAB如何在 MATLAB 中绘制动画图形
【发布时间】:2014-12-11 09:44:10
【问题描述】:

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

我找到了相关问题 HereHere,但它们并没有解决我的问题。

【问题讨论】:

  • 有很多不同的方法可以在您的链接中重现动画,但您的请求定义不够。没有人会从头开始写这样的东西,以后会被告知你想要一些不同的东西。根据您链接的答案尝试一些东西,然后发布您的代码并解释什么对您有效,什么仍然无效。 提示:要让图形四处移动,当您刷新时,您只需设置以您绘制的最后一点为中心的轴的限制。
  • 很好的提示。我会这样做并返回我的代码。谢谢。

标签: matlab animation figure


【解决方案1】:

感谢@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

【讨论】:

    【解决方案2】:

    使用fifo缓冲区释放旧点并添加新点。函数“drawnow”比“暂停”更快:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      相关资源
      最近更新 更多