我们正在尝试使用不同颜色和大小的点来模拟移动的汽车(汽车类型1:红色尺寸20,汽车类型2:绿色尺寸40等等),问题是如果有两辆相同类型的汽车他们它们应该相互跟随的重叠,这是使用过的代码:
x = linspace(0,30,1000);
axis([0,20,-0.4,1.5]);
ax = gca;
h = hgtransform('Parent',ax);
type1=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20);
type2=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40);
car=[1 2 2 1 1];
for k = 1:10:700
for i = 1:length(car)
if(car(i)==1)
set(type1,'XData',x(k),'YData',0.4);
pause(0.1);
elseif(car(i)==2)
set(type2,'XData',x(k)-3,'YData',0.4);
pause(0.1);
end
end
end
如何保持每辆(x = 3)新车开始移动的顺序而不删除或重叠前面的车辆。
非常感谢
答案 0 :(得分:1)
您可能需要以下代码。
x = linspace(0,30,1000);
axis([0,20,-0.4,1.5]);
ax = gca;
car=[1 2 2 1 1];
h = hgtransform('Parent',ax);
Ncar=length(car);
for n=1:Ncar %generate 5 cars having one type among type1 and type2
if car(n)==1;
types(n)=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20);
else
types(n)=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40);
end
end
for k = 1:10:700 %plotting the cars sequencially
for i = 1:length(car)
set(types(i),'XData',x(k)-3*(i-1),'YData',0.4);
pause(0.1);
end
end