Dynamic Plotting "Phantom" Line

2 views (last 30 days)
Chris Berry
Chris Berry on 28 Apr 2011
Dear fellow MATLAB Users,
I have created a dynamic plotting system to be used with various x and y data points and have unwanted line appearing at the start of the plotting sequence. Here is my code as it currently stands:
__________ CODE START __________
x=[0;6;2;8;4;10;6;12;8];
y=[0;2;4;6;8;10;12;14;16];
t=numel(x);
r=x(t);
nnn=y(t);
s1=subplot(1,1,1);
set(s1,'xlim',[1 numel(x)],'ylim',sort([0 r]),'nextplot','add')
p1=plot(r,nnn,'r-');
p2=plot(r,nnn,'g*');
axis([0 20 0 20])
set(gca,'ydir','rev')
for t=1:numel(x)
r=x(t);
nnn=y(t);
set(p1,'xdata',[get(p1,'xdata') r],'ydata',[get(p1,'ydata') nnn])
set(p2,'xdata',r,'ydata',nnn)
pause(.5)
end
__________ CODE END __________
Notice how there is a line extending from the origin, to the final data point. I cannot understand why this is occurring, nor what to do to remedy it. Any help that could be lent to solve this issue would be greatly appreciated.
Thank you,
~ Chris
  2 Comments
Jan
Jan on 28 Apr 2011
Please use the code formatting as described at the link called "Markup" on this page.
Chris Berry
Chris Berry on 28 Apr 2011
I apologize, I am not able to find the "Markup" link on this page, nor through a site search. Would you please provide me with a link so that I can better submit questions and answers? Thanks.

Sign in to comment.

Accepted Answer

Paulo Silva
Paulo Silva on 28 Apr 2011
Initialize p1 with this code
p1=plot(nan,nan,'r-');
instead of
p1=plot(r,nnn,'r-');
  1 Comment
Chris Berry
Chris Berry on 28 Apr 2011
Exactly! Thank you, Paulo, for your help. It is people like you that make this forum great!

Sign in to comment.

More Answers (1)

Jan
Jan on 28 Apr 2011
You get a connection from the origin to the last data point, because your program explicitely instructs that.
x = [0;6;2;8;4;10;6;12;8];
y = [0;2;4;6;8;10;12;14;16];
t = numel(x);
r = x(t);
nnn = y(t);
s1 = axes('xlim',[0, 20],'ylim',sort([0 20]), ...
'nextplot','add', 'ydir','rev');
p1 = plot(r, nnn, 'r-');
p2 = plot(r, nnn, 'g*');
% Now [p1] is the last data point
for t = 1:numel(x)
r = x(t);
nnn = y(t);
set(p1, 'xdata',[get(p1,'xdata') r], ...
'ydata',[get(p1,'ydata') nnn])
set(p2, 'xdata',r, 'ydata',nnn)
pause(.5)
% In the first iteration the origin is added to [p1]!!!
end
I cannot guess, what you want to do instead. Perhaps you want to start your t-loop from 2 or remove the zeros from x and y.
  2 Comments
Jan
Jan on 28 Apr 2011
Puh, the latency is cruel for me. I've typed the answer and waited 13 minutes until it appears completely in the web interface. Therefore I did not see Paulo's answer before.
Chris Berry
Chris Berry on 28 Apr 2011
Ha. I still appreciate you trying to help me. Take care.
~ Chris

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!