how to prevent scatter handle deletion!

조회 수: 5 (최근 30일)
john_arle
john_arle 2019년 10월 26일
답변: john_arle 2019년 10월 26일
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에서 Training and Simulation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by