多个移动物体同时

时间:2014-06-11 08:14:44

标签: matlab

我需要在MATLAB环境中同时移动多个对象。 我为三个物体移动制作了一个代码。但是,这些物体正在连续移动。我需要看到所有物体同时移动。

% Create data
t = -4:0.1:4;    % Time data
x = t; % Position data
y = x; % Position data

h = plot(x(1),0,'o','MarkerSize',20,'MarkerFaceColor','g');

xlim([-5,5]);
ylim([-5,5]);
grid on

% Animation Loop one
i = 1;

while i<=length(x)
    set(h,'XData',x(i), 'YData',y(i));

          drawnow;
     M(i) = getframe(1);
        i=i+1;

end

hold on

t = -4:0.1:4;    % Time data
x = t; % Position data
y = -x; % Position data
h = plot(x(1),0,'o','MarkerSize',20,'MarkerFaceColor','r');
i = 1;
while i<=length(x)
    set(h,'XData',x(i), 'YData',y(i));

          drawnow;
     M(i) = getframe(1);
        i=i+1;

end


hold on

t = -4:0.1:4;    % Time data
x = t; % Position data

h = plot(x(1),0,'o','MarkerSize',20,'MarkerFaceColor','b');
i = 1;
while i<=length(x)
    set(h,'XData',x(i));

          drawnow;
     M(i) = getframe(1);
        i=i+1;

end

2 个答案:

答案 0 :(得分:0)

外循环必须是时间(x是你的情况)并且内循环对象。这样他们就会被视为同时飞行。用一种抽象的表示法:

% outer loop over animation time
for time = 1 : last_time
  % inner loop over all objects
  for object = 1 : last_object
    % draw a specific object at a specific time
    draw(object, time);
  end
end

答案 1 :(得分:0)

也许你想试试这个:

string