Plot function for two moving points with different velocities

1 次查看(过去 30 天)
I have the following code but it gives an error undefined function or variable 'x'. Can anyone please help me in this? plot(x, y, 'b');
car = line([], [], 'MarkerSize', 10, 'Color', 'r');
carx = x(1); cary = y(1);
numxy = length(x);
aimidx = 2;
numv = length(v);
vidx = 0;
speed = 0;
while true
if speed <= 0
carm = 'x';
set(car, 'XData', carx, 'YData', cary, 'Marker', carm);
drawnow;
if vidx >= numv || aimidx > numxy
break; %done following path
end
vidx = vidx + 1;
speed = v(vidx);
continue;
end
deltax = x(aimidx) - carx;
deltay = y(aimidx) - cary;
aimdist = sqrt(deltax.^2 + deltay.^2);
if aimdist <= speed
carx = x(aimidx);
cary = y(aimidx);
aimidx = aimidx + 1;
speed = 0; %flag to draw current point and move to next v
continue;
end
carx = carx + speed * deltax ./ aimdist;
cary = cary + speed * deltay ./ aimdist;
if abs(deltax) >= abs(deltay)
if deltax > 0
carm = '>';
else
carm = '<';
end
elseif deltay > 0
carm = '^';
else
carm = 'v';
end
set(car, 'XData', carx, 'YData', cary, 'Marker', carm);
drawnow;
end

回答(1 个)

Image Analyst
Image Analyst 2017-11-21
If the first line of your code is this:
plot(x, y, 'b');
then why do you think x and y should have any values when you call plot()? You need to set them equal to something before you call plot.

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by