Dot moves from corner to corner (around monitor) in matlab

84 views Asked by At

I am trying to do create a ball or dot or whatever that goes from left bottom corner to right bottom corner then top right corner then left top corner then on the start so left bottom corner. Basically around the screen. And in total fullscreen that important. For fullscreen graph i am using WindowAPI.

So here is my code.

try
   % Create a figure to operate on: --------------------------------------------
   % The OpenGL renderer is confused by the alpha blending, so Painters is used:
   FigH   = figure('Color', ones(1, 3), 'Renderer', 'Painters');
   axes('Visible', 'off', 'Units', 'normalized', 'Position', [0, 0, 1, 1]);
   % Set topmost status:
   WindowAPI(FigH, 'topmost');   % Command is not case-sensitive
   drawnow;
   WindowAPI(FigH, 'TopMost', 0);
   drawnow;
   WindowAPI(FigH, 'front');
   drawnow;
   
   % Nicer to have the figure on topmost for the rest of the demo:
   WindowAPI(FigH, 'topmost');
   
   % Special maximizing such that the inner figure fill the screen:
   WindowAPI(FigH, 'Position', 'full');  % Complete monitor
   
    % START MOVING BALL

X = 2;
Y = 0;
for i=1:1490
    X = X + 0.1;
    Y = 2
    plot(X,Y,'or','MarkerSize',20,'MarkerFaceColor','r')
    axis([0 151 0 85])  
    pause(0)
end
   % END MOVING BALL
   
end

enter image description here

For simplicity that dot goes only from left to right bottom corner. But there are two problems.

  1. that dot lag sometimes which is a problem.
  2. There are visible black lines (from the graph).

And I don't know how to fix these two problems. So if u know how to fix them or any better way how to animate ball in Matlab please post it here. Thanks for your time.

1

There are 1 answers

0
Daniel Aldrich On BEST ANSWER

To reduce the amount of 'Lag' you can do a few of the following:

  1. Move the plot line outside of the loop and assign it to a handle, additionally move the axis commandos out with it

    pH = plot(X,Y,'or','MarkerSize',20,'MarkerFaceColor','r');
    axis([0 151 0 85])  
    axis off
    

    Now you can update the X Y data directly in the loop.

    set(pH,'XData',X,'YData',Y)
    
  2. Replace the pause with (replace 0 with the length in seconds that you want to delay the frame by):

    tic;while toc<0;end
    

    This is faster, more precise, and does not cause an interrupt to MatLab.

  3. Add the drawnow command in the loop to cause the figures to update.