# Plotting position every 10 secound

4 views (last 30 days)
Mikkel on 23 Mar 2015
Commented: Mikkel on 24 Mar 2015
I have this code that plots two objects moving around. I would like to add such that it marks the position of both objects for every 10-20 times it runs through. How Do I do that?
axis([-0,400,-200,200])
hold on
% Data
x1 = squeeze(x_V);
y1 = squeeze(y_V);
N1 = NaN(size(x1));
x2 = squeeze(x_O);
y2 = squeeze(y_O);
N2 = NaN(size(x2));
% Create the line and get its X and Y data
h1 = plot(N1, N1);
h2 = plot(N2, N2);
xdata1 = get(h1, 'XData');
ydata1 = get(h1, 'YData');
xdata2 = get(h2, 'XData');
ydata2 = get(h2, 'YData');
for k = 1:length(x1)
xdata1(k) = x1(k);
ydata1(k) = y1(k);
xdata2(k) = x2(k);
ydata2(k) = y2(k);
% Update the plot
set(h1, 'XData', xdata1, 'YData', ydata1, 'Color', 'red');
set(h2, 'XData', xdata2, 'YData', ydata2, 'Color', 'blue');
drawnow
end

Mikkel on 23 Mar 2015
Adding this after the first loop gave me somehow the result that I needed, but it is not a nice piece of code..
axis([-0,400,-200,200])
hold on
% Data
x1 = squeeze(x_V);
y1 = squeeze(y_V);
N1 = NaN(size(x1));
x2 = squeeze(x_O);
y2 = squeeze(y_O);
N2 = NaN(size(x2));
% Create the line and get its X and Y data
h1 = plot(N1, N1);
h2 = plot(N2, N2);
xdata1 = get(h1, 'XData');
ydata1 = get(h1, 'YData');
xdata2 = get(h2, 'XData');
ydata2 = get(h2, 'YData');
for k = 1:length(x1)
xdata1(k) = x1(k);
ydata1(k) = y1(k);
xdata2(k) = x2(k);
ydata2(k) = y2(k);
% Update the plot
set(h1, 'XData', xdata1, 'YData', ydata1, 'Color', 'red');
set(h2, 'XData', xdata2, 'YData', ydata2, 'Color', 'blue');
drawnow
end
for k = 1:100:length(x1)
xdata1(k) = x1(k);
ydata1(k) = y1(k);
xdata2(k) = x2(k);
ydata2(k) = y2(k);
plot(xdata1(k),ydata1(k),'ro')
plot(xdata2(k),ydata2(k),'bo')
end

Image Analyst on 23 Mar 2015
Try rem() or mod(). Like "if rem(k, 10)" inside the loop to plot only every 10th time.

Mikkel on 23 Mar 2015
That just gives me a line of markerswhen I did like this
if rem(k,5000)
plot(xdata1(k),ydata1(k),'ro')
end
the same for mod
Image Analyst on 23 Mar 2015
Did you use "hold on"? If you want lines between the markers, use 'ro-'. You can adjust the line width with the 'LineWidth' option of plot().
Mikkel on 24 Mar 2015
yes, hold on is on... (see the 2nd line). The problem is that the rem() or mod() plots every point, and not only the wanted ones. The code I provided below gives me the result that I want. but it is just not a nice way to do it.
On a related note. Is it possible to plot numbers, like numbering the marker 1, 2, ..., n ? and how would that be done?