Connecting points in graph

조회 수: 12 (최근 30일)
arisish roy
arisish roy 2016년 2월 27일
편집: Stephen23 2016년 2월 27일
I have two matrices x and y with 1x5 order. The x matrix gives x co-ordinates and y matrix gives y co-ordinates for points in the plot. So I have a set of 5 points on the graph. How do I connect these points on the graph? I have used for loop and lines equation
for i=1:5
line([x(i),y(i)], [x(i+1),y(i+1)],'LineStyle', '-');
end
but this is not giving me the correct lines. Can you please help?

채택된 답변

Muhammad Usman Saleem
Muhammad Usman Saleem 2016년 2월 27일
편집: Muhammad Usman Saleem 2016년 2월 27일
you have not define vectors x(i),y(i) before plot
Lets take an example
A=[1 2 3 4 5; 6 7 8 9 10];% give here your matrix of x
B=[ 20 21 22 23 24 ; 25 27 28 29 30]; % give here matix y
for i=1:size(A,2)
figure(i)
plot(A(i),B(i))
end
This will run nicely when you give more detail of your questoin

추가 답변 (1개)

Stephen23
Stephen23 2016년 2월 27일
편집: Stephen23 2016년 2월 27일
It is not creating the lines correctly because you are using line incorrectly. The documentation clearly gives this basic syntax:
line(X,Y)
whereas somehow you have invented a new syntax that passes the coordinates for two points:
line([x1,y1],[x2,y2]) % this is not valid MATLAB syntax
Solution: you do not need a loop anyway, just do this:
line(x,y,'LineStyle', '-');

카테고리

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