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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Benjamin Kraus
Benjamin Kraus il 22 Mar 2018
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 Commento
Benjamin Kraus
Benjamin Kraus il 22 Mar 2018
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Vector Fields in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by