Info

此问题已关闭。 请重新打开它进行编辑或回答。

Scatter doen not plot!

1 次查看(过去 30 天)
john_arle
john_arle 2019-10-27
关闭: MATLAB Answer Bot 2021-8-20
Hello!
The code lines written below are inserted in a for loop. Depending on whether
my figure has been initialized or not, the former or the latter part of the code is
executed. However, after the first iteriation (where the figure is initialized),
points disappear from the figure and do not come back.
As you can see, I used the instruction drawnow, but it seems useless.
I hope someone of you guys knows how to solve my issue!
Best,
John_Arlecchino.
if self.plot_initialized == 0 % Initializing figure
self.figure_handle = figure; clf;
grid on;
colours = swarm.get_colors();
size_agents = repmat(30, swarm.nb_drones, 1);
pn = x_current(1, :);
pe = x_current(2, :);
pd = x_current(3, :);
self.scatter_handle = scatter3(pe, pn, -pd, size_agents, colours', 'filled');
for agent = 1:swarm.nb_drones
self.wake = zeros(10, 3, swarm.nb_drones);
for i = 1:10
self.wake(i, 1:3, agent) = x_current(1:3, agent);
end
end
xlabel('Y position [m]');
ylabel('X position [m]');
zlabel('Z position [m]');
xlim([-200 300])
ylim([-200 300])
axis square;
view(0,90);
grid on;
hold on;
self.plot_initialized = 1;
%----------------------------------------------------------
else
size_agents = repmat(30, swarm.nb_drones, 1);
colours = swarm.get_colors();
pn = x_current(1, :);
pe = x_current(2, :);
pd = x_current(3, :);
set(self.scatter_handle, 'Xdata', pe, 'Ydata', pn, ... % Here it is where points on plot disappear!
'Zdata', -pd, 'Marker', '.', 'SizeData', ...
size_agents, 'CData', colours');
xlabel('Y position [m]');
ylabel('X position [m]');
zlabel('Z position [m]');
axis square;
view(0,90);
grid on;
hold on;
colour_wake = 200*ones(10, 3);
for agent = 1:swarm.nb_drones
self.wake = circshift(self.wake, 1);
self.wake(1, :, agent) = x_current(:, agent);
self.lines_handle(agent) = scatter(...
self.wake(1:end, 2, agent), ...
self.wake(1:end, 1, agent), 4, colour_wake);
hold on;
drawnow;
end
end
  2 个评论
Rik
Rik 2019-10-27
Your setting hold on after you created a new plot with the scatter function. You also aren't using any explicit parent when creating your graphical elements. This is probably fine in the initialization phase (as the user probably wouldn't select another figure during that part), but especially in the second part of your code, this is required for a robust GUI design.
The drawnow function forces a graphics update (and the execution of any queued callbacks), so that can be helpful if you want to show the user something changing in a loop, but it does not actually create new graphical elements.
I hope this helps.
john_arle
john_arle 2019-10-27
Thank you both! Could you please show me how to correct the code? I mean, how should I create new object then?
Thank you.

回答(1 个)

Daniel M
Daniel M 2019-10-27
编辑:Daniel M 2019-10-27
Put this line outside the loop over swarm.nb_drones, or only the last iteration will be non zero.
self.wake = zeros(10, 3, swarm.nb_drones);
  1 个评论
john_arle
john_arle 2019-10-27
编辑:john_arle 2019-10-27
Thank you Daniel!
I had not noticed!

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by