Plot a moving dot inside a quiver plot that is constantly being updated

1 回表示 (過去 30 日間)
Raldi
Raldi 2018 年 3 月 22 日
コメント済み: Benjamin Kraus 2018 年 3 月 22 日
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 件のコメント
KSSV
KSSV 2018 年 3 月 22 日
Most of the variables are not defined......we cannot run the code.
Raldi
Raldi 2018 年 3 月 22 日
Updated the code so it can now run

サインインしてコメントする。

採用された回答

Benjamin Kraus
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
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 ExchangeVector Fields についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by