Search code examples
matlabplotmovie

Move a dot across screen video


I'm having trouble in figuring out how to move a plotted dot across the screen in a movie. So let's say I plot a point:

plot(-1.4,-1.4, '.k', 'MarkerSize', 20)

and initialize a video object

vidObj = VideoWriter('dot.avi');
vidObj.Quality = 100;
vidObj.FrameRate = 10;
open(vidObj);

then how do I make the dot move to the right of the screen?


Solution

  • To make the dot moving across the right you can write a loop which plots the dot; at each iteration the X coordiantes of the point are incremented.

    Within the loop you also have to capture each frame ad add it to the video file.

    % Define the initial point's coords
    P=[-1.4,-1.4];
    % First plot
    plot(P(1),P(2), '.k', 'MarkerSize', 20)
    % Initialize the video
    vidObj = VideoWriter('dot.avi');
    vidObj.Quality = 100;
    vidObj.FrameRate = 10;
    open(vidObj);
    % set axis properties
    set(gca,'nextplot','replacechildren');
    % Set xlim (max vaalue to be set according to the desired final position of
    % the point)
    set(gca,'xlim',[P(1)*2 150]);
    % Loop for the point movement: 100 step, each iteration the pont in moved
    % to the right of 1x (where x is the unito of measure of the space)
    for i = 1:100
       plot(P(1)+i,P(2), '.k', 'MarkerSize', 20)
    % Capture the current frame   
       currFrame = getframe;
    % Add the current frame to the video file  
       writeVideo(vidObj,currFrame);
    end
    % Close the video file (stop recording)
    close(vidObj);
    

    Hope this helps.