Make a point move straight up and down

7 views (last 30 days)
SS
SS on 15 Aug 2020
Edited: SS on 15 Aug 2020
Hi,
I would like to know how to plot a point and just make it go up, down, and back to the starting point. I was thinking about making a list of y values but was not sure if there was a simpler way.
Thanks

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 15 Aug 2020
Hi,
A simple motion or animation tool with plot tools is drawnow that can be employed in your task, e.g.:
  2 Comments
SS
SS on 15 Aug 2020
Edited: SS on 15 Aug 2020
I tried it out and it does not seem to show the up and down motion. Is there something I am missing?
SS
SS on 15 Aug 2020
Edited: SS on 15 Aug 2020
x = 0:.01:50; %linspace of x
y = sin(x); %wave equation
px = 0; %initial x plot
py = 0; %initial y plot
for i=1:1000 %loop
set(gcf,'DoubleBuffer', 'on');
h = patch([0 1 0 1], [0 1 1 0], 'r');
hold on
figure(100); %so code will replot over the previous figure, and not make a new one.
hold off
py = y(i)
plot(x,y, px, py,'o'); %circle point
pause(0.02); %speed of moving point
end
Here is what I have. But when I overlay an image on it, it flickers. I also want to get rid of the sine wave, and just keep the point moving up and down.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 15 Aug 2020
Hi,
If I've understood your question correctly, you'd like to get a up and down motion of a point x, y(i). Then here is one of the possible animated plots with drawnow:
for ii=1:numel(x) %loop
plot(x,y, '--', px, y(ii),'o', 'markerfacecolor', 'y'); %circle point
hold off
drawnow
end
  1 Comment
SS
SS on 15 Aug 2020
Edited: SS on 15 Aug 2020
Hi, thank you! that works. However, additionally, I wanted to show the dot moving on a picture that I uploaded, so I want to hide the sine wave and just show the dot. When I add the picture, the whole thing flickers. How would I hide both the wave and prevent the flickering? You can see the code I have in the above comment. I apologize for all the questions.

Sign in to comment.

Categories

Find more on Animation 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!