how to prevent scatter handle deletion!

6 次查看(过去 30 天)
Hello!
In a for loop, I first initialize a scatter plot, and then I update it.
However, when adding a second handle (lines_handle) to my code,
the former (scatter_handle) tells me that the object scatter has been deleted.
It is probably because I am assigning another scatter plot to lines_handle.
I believe this should not happen and most of all I do not know how to prevent it.
Thank you.
You can see my code below, look at the only comment to see where the scatter object is deleted!
if self.plot_initialized == 0
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]');
axis square;
view(0,90);
grid 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, ...
'Zdata', -pd, 'Marker', '.', 'SizeData', ...
size_agents, 'CData', colours');
colour = 200*ones(10, 3);
for agent = 1:swarm.nb_drones
self.scatter_handle
agent
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);
% Here it is where scatter_handle gets deleted!
self.scatter_handle
end
drawnow;
end
title(sprintf('Simulation time: %.2f seconds', time));
  1 个评论
john_arle
john_arle 2019-10-26
编辑:john_arle 2019-10-26
I had to add hold on instruction after the first scatter instruction. Otherwise, Matlab next scatter deletes the previous one no matter what.
Thank you, John!
if self.plot_initialized == 0
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]');
axis square;
view(0,90);
grid on;
hold on; % HERE IT IS WHERE I INSERTED A HOLD ON LINE!
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, ...
'Zdata', -pd, 'Marker', '.', 'SizeData', ...
size_agents, 'CData', colours');
colour = 200*ones(10, 3);
for agent = 1:swarm.nb_drones
self.scatter_handle
agent
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);
% Here it is where scatter_handle gets deleted!
self.scatter_handle
end
drawnow;
end
title(sprintf('Simulation time: %.2f seconds', time));

请先登录,再进行评论。

采纳的回答

john_arle
john_arle 2019-10-26
See my comment above.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Import Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by