我想在 matlab 图形上绘制一个移动点,但我需要在绘制该点的新位置时移动图形。更清楚的是,我想达到这个效果(移动车辆)。
我在这里和这里找到了相关的问题,但它们不能解决我的问题。
感谢@ Hoki
的提示,我解决了这个问题。
ff
是大小为 (n*2) 的 n
个数据点 (x,y) 的数组。由于我的点在步长 0.001 的窄范围内,因此我使用这些特定值的 dx
和dy
。这取决于点步骤。
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
使用 fifo 缓冲区释放旧点并添加新点。函数"drawnow"比"pause"更快:
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