Plot function for two moving points with different velocities
    3 views (last 30 days)
  
       Show older comments
    
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
0 Comments
Answers (1)
  Image Analyst
      
      
 on 21 Nov 2017
        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.
0 Comments
See Also
Categories
				Find more on Analysis of Variance and Covariance in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
