How can the arrow remain on the graph whilst the the other plots are produced?

2 vues (au cours des 30 derniers jours)
Obed James
Obed James le 20 Juin 2021
i have this code which creats an arrow. However it doesn't work when combined with my code for an animated plot.
x = [-1 , 1/3, 1/3, 1, 1/3, 1/3,-1 ];
y = [-1/3,-1/3,-1/2, 0, 1/2, 1/3, 1/3];
g = hgtransform;
patch('XData',x,'YData',y,'FaceColor','red','Parent',g)
This one simulates movement
close all
clear
clc
disp ('The terrain has many obsticles which your robot must a manuver around to move forward')
prompt = 'How far forward, in meters, did your bot tranvel? ';
n = input (prompt);
if n <= 0
disp 'Objects are not permitted to move backwards. Please retry'
end
if n >= 1
%% Oject coordinates
t = 0:0.1:n;
y = sin(t);
y2 = cos(t);
for k = 1:length(t)
%% Marker plots
plot(t(k),y(k),'x')
hold on
plot(t(k),y2(k),'o')
hold on
%% Line plots
plot(t(1:k),y(1:k))
hold on
plot(t(1:k),y2(1:k))
%% Graph properties
axis([0 n*1.0005 -1 1]) %Allows for easier viewing of the plotting and sets a focus over these points of the graph
view(2); % Changes the default/2d to a 3 dimensional graph
grid on % Turns on the grid lines for the graph
xlabel('Forward Movement') %Labels the x axis
ylabel('Left and Right Movement') %Labels the y axis
legend('sin(t) marker', 'cos(t)marker', 'sin(t)','cos(t)') % Adds a key to the corner graph.
pause(0.1) % Pauses the complete plotting of the graph and allows it to happen one at a time with each frame.
if k~=length(t)
clf %Clears all the x and o markers from the frame of the graph,except for the last points
end
end
end

Réponses (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 20 Juin 2021
close all % already takes care of your issue
To avoid overlapping of the animated plot with legend, you may need to consider this syntax:
legend('sin(t) marker', 'cos(t) marker', 'sin(t)','cos(t)', 'location', 'best')

Walter Roberson
Walter Roberson le 20 Juin 2021
clear
You are relying on the fact that clear does not remove graphic objects and so will delete the variable named g that you already created, but will not remove the patch object ???
clf %Clears all the x and o markers from the frame of the graph,except for the last points
You need to understand clf as meaning the equivalent of
delete(findall(gcf))
Which is to say that your clf() call is definitely going to delete the patch() object you created. Oh, and it is also going to delete the "last points", no "except" about it. But it is going to delete all axes, all colorbars, all legends, all uicontrol, all other graphics objects inside the figure.
I would suggest you create a small number of graphics objects:
  1. One patch() object
  2. One line() object with markers
  3. One animatedline object
Then, each cycle, update the XData and YData properties of the line() object in order to move the marker, and use addpoints() to update the "trail" that you are drawing.
No plot() inside the activity loop, no clf() inside the activity loop. Nothing other than updating properties of existing objects, and drawnow() to ask the screen to be updated.

Catégories

En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by