MATLAB Answers

Connecting line between different points obtained from a for-loop

102 views (last 30 days)
AHN
AHN on 12 Oct 2016
Commented: AHN on 29 Nov 2016
Hello, I have obtained a plot of set of points from a for-loop as shown above. I simply want to add a line connecting these points. The function plot(x,y,'-*') does not work since these points are from different iterations. Please can someone help. Thanks.
  2 Comments
AHN
AHN on 13 Oct 2016
Thank you for the reply. It's still not working on my problem, but good to know various approaches to the pseudo-code. I will keep trying, Thanks.

Sign in to comment.

Answers (3)

Pritesh Shah
Pritesh Shah on 12 Oct 2016
This will work for your case !!
All the best !!
  1 Comment
AHN
AHN on 12 Oct 2016
Thank you for the reply. I still cannot get a connecting line through the points. Here's my pseudo-code. Thanks.
for x=1:1:20;
y=x+2
hold all
plot(x,y,'*')
end

Sign in to comment.


Thorsten
Thorsten on 12 Oct 2016
x = 1:20;
y = x+2;
plot(x,y,'*-')
  2 Comments
Thorsten
Thorsten on 14 Oct 2016
It would be helpful if you post a complete yet minimal example that does not work.

Sign in to comment.


Image Analyst
Image Analyst on 14 Oct 2016
These work:
% Plot all at once:
subplot(2, 1, 1);
x = 1 : 20;
y = zeros(1, length(x));
for i = 1 : length(x)
y(i) = x(i) + 2;
end
plot(x,y,'b-*')
grid on;
% Plot a point at a time:
subplot(2, 1, 2);
grid on;
ylim([0, 22]);
xlim([0, 20]);
hold on;
x = 1 : 20;
y = zeros(1, length(x));
for i = 1 : length(x)
y(i) = x(i) + 2;
plot(x(1:i),y(1:i),'b-*')
drawnow;
pause(0.3);
end
Of course you could also vectorize and do away with the for loops
x = 1 : 20;
y = x + 2;
plot(x,y,'b-*')
grid on;
  4 Comments
AHN
AHN on 29 Nov 2016
z1 =
0.6510
0.6995
-0.2816
-0.1782
1.7098
0.1839
1.0352
-0.5937
0.3116
0.2233
z2 =
1.7924
-1.3664
0.0013
0.0541
-1.3397
1.3086
1.1972
-0.1535
0.7780
-1.0430

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!