Plot a moving dot inside a quiver plot that is constantly being updated
조회 수: 2 (최근 30일)
이전 댓글 표시
I have the following code that will create a quiver plot (imagine a team of players in a football game) and I would like to update the position of the ball (a red dot).
x = rand(10);
y = rand(10);
direction = 3*rand(10);
u = sin(direction);
v = cos(direction);
H = quiver(x, y, u, v, 'filled', 'Marker', 'o', 'LineWidth', 1.8, 'AutoScaleFactor', .1);
for c = 0:100
% make a step
x = x + 5 * sin(direction);
y = y + 5 * cos(direction);
Xball = 10*rand();
Yball = 10*rand();
u = sin(direction);
v = cos(direction);
pause(0.5);
set(H, 'XData', x, 'YData', y, 'udata', u,'vdata', v, 'MarkerFaceColor', 'b');
%scatter(H, Xball, Yball, 12, 'm', 'filled'); % will complain about the axes
hold on;
scatter(Xball, Yball, 12, 'm', 'filled'); % will work but will keep showing all the balls
hold off;
drawnow;
end
The quiver plot will update correctly but I cannot add the ball, and make it change position on each iteration. What is the correct way of doing it?
댓글 수: 2
채택된 답변
Benjamin Kraus
2018년 3월 22일
Regarding this line of code:
scatter(H, Xball, Yball, 12, 'm', 'filled'); % will work but will keep showing all the balls
Each call to scatter will create a new scatter object. What you want to do is to create a single scatter object and then update the data in that single object. Something closer to this:
s = scatter(H, Xball, Yball, 12, 'm', 'filled'); % Create onces
for c = 0:game
s.XData = Xball; % Update the data of the existing object in every loop
s.YData = Yball;
end
It also looks you are trying to use H as both an axes and a quiver object. The scatter command can take an axes object as a first input, but an axes object does not have an XData or YData property.
I cannot run your code without more of the variables defined, but I attempted to update it based on what I think you are trying to do:
H = quiver(NaN, NaN, NaN, NaN) % Replace with however you are creating your quiver plot.
AX = H.Parent; % Get the axes that is the parent of the quiver plot.
hold(AX,'on')
S = scatter(NaN, NaN, 12, 'm', 'filled'); % Create a scatter plot.
hold(Ax,'off')
for c = 0:game
% make a step
x(3:end) = x(3:end) + speed * sin(direction);
y(3:end) = y(3:end) + speed * cos(direction);
% packet transmission
Xball = rand();
Yball = rand();
% update u v components
u = sin(direction);
v = cos(direction);
% update graph
pause(0.5); % Note: Pause will also call |drawnow|
set(H, 'XData', x(3:end), 'YData', y(3:end), 'udata', u,'vdata', v, 'MarkerFaceColor', 'b');
set(S, 'XData', Xball, 'YData', Yball);
drawnow;
end
댓글 수: 1
Benjamin Kraus
2018년 3월 22일
New version of my code based on your updated code:
x = rand(1,10);
y = rand(1,10);
direction = 3*rand(1,10);
u = sin(direction);
v = cos(direction);
H = quiver(x, y, u, v, 'filled', 'Marker', 'o', 'LineWidth', 1.8, 'AutoScaleFactor', .1);
AX = H.Parent; % Get the axes that is the parent of the quiver plot.
hold(AX,'on')
S = scatter(NaN, NaN, 12, 'm', 'filled'); % Create a scatter plot.
hold(AX,'off')
for c = 0:100
% make a step
x = x + 5 * sin(direction);
y = y + 5 * cos(direction);
Xball = 10*rand();
Yball = 10*rand();
u = sin(direction);
v = cos(direction);
% update graph
pause(0.5); % Note: Pause will also call |drawnow|
set(H, 'XData', x(3:end), 'YData', y(3:end), 'udata', u(3:end),'vdata', v(3:end), 'MarkerFaceColor', 'b');
set(S, 'XData', Xball, 'YData', Yball);
drawnow;
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Vector Fields에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!