Plotting position every 10 secound

조회 수: 1 (최근 30일)
Mikkel
Mikkel 2015년 3월 23일
댓글: Mikkel 2015년 3월 24일
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
Mikkel 2015년 3월 23일
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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 3월 23일
Try rem() or mod(). Like "if rem(k, 10)" inside the loop to plot only every 10th time.
  댓글 수: 3
Image Analyst
Image Analyst 2015년 3월 23일
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
Mikkel 2015년 3월 24일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by