- You are calling hold on then following it with clf. clf is going to clear the entire figure, which negates any value to having hold on. You probably don't need or want clf anywhere in this script, but if you have it, call it once at the top of the entire script. By default (when hold is 'off'), calling plot will clear the contents of the axes, so if you want the axes to be cleared, then you can remove both the hold on and clf and each call to plot will replace the previous contents of the axes.
- The syntax you are using for plot will create two identical lines, one with 300 points and no markers and the other with a single point and either a circle or plus marker. They will both have MarkerSize of 5 (or 7) and MarkerFaceColor of red (or blue), but because the first line doesn't have any markers this doesn't impact the display, but it can be a bit confusing. I recommend breaking that into two calls to plot, one to make your "fixed" complete curve and one to make the moving marker(s).
- MATLAB isn't going to create a picture until you call either pause or drawnow. This means that your entire first loop is running without actually causing any display. If your intention was to have a marker run from one end of the line to the other, then repeat again, you just need to add either pause or drawnow to the first loop. The animation you see when you run your code is entirely due to the second for loop.
- You are setting the MarkerFaceColor to blue, but a plus marker has no face. You want to set the MarkerEdgeColor to blue.

# I want to draw 2 moving points in a graph. Please help with the easiest code as I m just a beginner.I tried with x(i), y(i) but only 1 moving point I could get. Please help

1 view (last 30 days)

Show older comments

My code that I tried for 2 moving pointers

z=linspace(0,10*pi,300); t=1:300; axis([0 10*pi -5 5]); x=3*cos(0.01*(t-z)).*cos(t-5*z); for i=1:200

clf

plot(z,x,z(i),x(i),'o','MarkerSize',5,'MarkerFaceColor','r');

hold on

end

for j=1:200

clf

plot(z,x,z(j),x(j),'+','MarkerSize',7,'MarkerFaceColor','b');

hold on

pause(0.05);

end

##### 0 Comments

### Accepted Answer

Benjamin Kraus
on 18 Nov 2021

Edited: Benjamin Kraus
on 18 Nov 2021

There are a few mistakes you are making in your script.

Both your loops are looping from 1:200, so I'm not entirely sure what your goal was. For the sake of showing a working example, I'm going to show you two versions of the code that will have two markers running in from each end of the line.

The first example is the "quick and dirty" approach, but only requires a small tweak to your current code:

N = 300;

z=linspace(0,10*pi,N);

t=1:N;

x=3*cos(0.01*(t-z)).*cos(t-5*z);

for i = 1:N

% Because hold is off at the beginning of the loop, this call to plot

% will clear the existing line from the axes and reset the axes

% limits.

plot(z,x)

% Turn hold on so that the second two calls to plot don't clear the

% axes.

hold on

plot(z(i),x(i), 'o', 'MarkerSize',5,'MarkerFaceColor','r');

plot(z(N+1-i), x(N+1-i), '+', 'MarkerSize',7,'MarkerEdgeColor','b');

% This should come after the calls to plot so that the axes limits are

% not reset by the call to plot.

axis([0 10*pi -5 5])

% Turn hold back off so the next loop replaces the entire axes contents.

hold off

% Pause (or drawnow) is required to make sure you see something from

% this frame.

pause(0.05)

end

The first version of the code is inefficient, because it requires deleting and recreating the fixed line every frame, and deleting and recreating the markers as well. You can make this much more efficient by creating the lines once and then setting the XData and YData on the lines to make them move.

N = 300;

z=linspace(0,10*pi,N);

t=1:N;

x=3*cos(0.01*(t-z)).*cos(t-5*z);

% Create your fixed line.

plot(z,x)

axis([0 10*pi -5 5]) % This should come after the call to plot

hold on

% Create another two lines for your markers.

m1 = plot(z(1),x(1), 'o', 'MarkerSize',5,'MarkerFaceColor','r');

m2 = plot(z(end),x(end), '+', 'MarkerSize',7,'MarkerFaceColor','b');

% Loop and move the two markers:

for i = 1:N

set(m1, 'XData', z(i), 'YData', x(i))

set(m2, 'XData', z(N+1-i), 'YData', x(N+1-i))

pause(0.05)

end

### More Answers (1)

Mathieu NOE
on 18 Nov 2021

hello

try this

N = 300;

z=linspace(0,10*pi,N);

t=1:N;

x=3*cos(0.01*(t-z)).*cos(t-5*z);

%// initiallize plot. Get a handle to graphic object

figure(1);

p = plot(NaN,NaN,'db');

axis([0 10*pi -5 5]); % freeze axes to their final size, to prevent Matlab from rescaling them dynamically

for ci = 1:N

% update the plot

pause(0.05)

set(p, 'XData', z(ci), 'YData', x(ci));

end

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!