Figure does not show on the plot

조회 수: 5 (최근 30일)
Mikkel
Mikkel 2015년 4월 21일
댓글: Adam 2015년 4월 21일
I have this function that plots the position of two boats as they are on a collision course. I want to add a small figure with a set time in between to illustrate the bearing of the vessels, and to make a comparison in time, and not only in space. I have the following function, and it makes the first vessel, (currently only one of them) but none of the next ones show up. For testing perpose i want the rest of the vessels to be angled 45deg.
The question Why does it only plot the first boat, and How do I make it shot the rest?
function Copy_of_plot_2_boats(x_V,y_V,x_O,y_O)
close all
axis([0,800,-400,400])
hold on
xlabel('x')
ylabel('y')
% vessel
Yvessel = [ 4 ; 6 ; 6 ; 5 ; 0 ; -5 ; -6 ; -6 ; -4];
Xvessel = [ -4 ; 0 ; 6 ; 10 ; 17 ; 10 ; 6 ; 0 ; -4];
% Data
x1 = squeeze(x_V);
y1 = squeeze(y_V);
N1 = NaN(size(x1));
x2 = squeeze(x_O);
y2 = squeeze(y_O);
N2 = NaN(size(x2));
% Create the line and get its X and Y data
h1 = plot(N1, N1);
h2 = plot(N2, N2);
xdata1 = get(h1, 'XData');
ydata1 = get(h1, 'YData');
xdata2 = get(h2, 'XData');
ydata2 = get(h2, 'YData');
for k = 1:length(x1)
xdata1(k) = x1(k);
ydata1(k) = y1(k);
xdata2(k) = x2(k);
ydata2(k) = y2(k);
% Update the plot
set(h1, 'XData', xdata1, 'YData', ydata1, 'Color', 'red');
set(h2, 'XData', xdata2, 'YData', ydata2, 'Color', 'blue');
drawnow
end
xdata1(1) = x1(1);
ydata1(1) = y1(1);
xdata2(1) = x2(1);
ydata2(1) = y2(1);
plot(xdata1(1),ydata1(1),'r*','MarkerSize',12)
plot(xdata2(1),ydata2(1),'b*','MarkerSize',12)
hndlList(1)= fill(Xvessel,Yvessel,'y','LineWidth',1);
for k = 100:200:length(x1)
xdata1(k) = x1(k);
ydata1(k) = y1(k);
xdata2(k) = x2(k);
ydata2(k) = y2(k);
% draw vessel hull
[PosXvessel,PosYvessel] = rot(Xvessel,Yvessel, 45);
PosXvessel = PosXvessel + xdata1(k);
PosYvessel = PosYvessel + ydata1(k);
set(fill(Xvessel,Yvessel,'y','LineWidth',1));
drawnow
% plot(xdata1(k),ydata1(k),'ro','MarkerSize',7)
% plot(xdata2(k),ydata2(k),'bo','MarkerSize',7)
end
function [PosX,PosY] = rot(X,Y,angle)
dim = size(X,1);
Mrot = [ cos(angle) -sin(angle) ;
sin(angle) cos(angle) ];
for i=1:dim
Pos_i = Mrot * [ X(i,1) ; Y(i,1) ];
PosX(i,1) = Pos_i(1,1);
PosY(i,1) = Pos_i(2,1);
end
legend('Vessel','Obstacle')

답변 (1개)

Adam
Adam 2015년 4월 21일
doc hold
I would strongly recommend using explcit axes handles, but simply
hold on
will cause the next plot to add to the previous on teh current axes rather than replace it.
  댓글 수: 3
Mikkel
Mikkel 2015년 4월 21일
Sorry, It turned out to be a question of assigning the wrong variable. Proving once again than asking for help alone can solve the problem...
Adam
Adam 2015년 4월 21일
Ah sorry, I missed that call on the 4th line. I usually put my hold instruction after the first plot instruction so I guess I was naturally looking around there!
hold( hAxes, 'on' )
would use an explcit axes handle, hAxes. It shouldn't change the functionality, but makes it more robust than just assuming the current axes is the correct one rather than the user happening to click on some other figure in the middle of the function running.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by