matlabplotpointsequential

Sequential connecting points in 2D in Matlab


I was wondering if you could advise me how I can connect several points together exactly one after each other.

Assume:

data =
          x        y
      ------------------
      591.2990  532.5188
      597.8405  558.6672
      600.0210  542.3244
      606.5624  566.2938
      612.0136  546.6825
      616.3746  570.6519
      617.4648  580.4575
      619.6453  600.0688
      629.4575  557.5777
      630.5477  584.8156
      630.5477  618.5906
      639.2696  604.4269
      643.6306  638.2019
      646.9013  620.7697
      652.3525  601.1584

"data" is coordinate of points.

Now, I would like to connect(plot) first point(1st array) to second point, second point to third point and so on.

Please mind that plot(data(:,1),data(:,2)) will give me the same result. However, I am looking for a loop which connect (plot) each pair of point per each loop.

For example:

data1=data;
figure
scatter(X,Y,'.')
hold on
for i=1:size(data,1)
[Liaa,Locbb] = ismember(data(i,:),data1,'rows');
data1(Locbb,:)=[];

[n,d] = knnsearch(data1,data(i,:),'k',1);
x=[data(i,1) data1(n,1)];
y=[data(i,2) data1(n,2)];
plot(x,y);
end
hold off

Although, the proposed loop looks fine, I want a kind of plot which each point connect to maximum 2 other points (as I said like plot(x,y))

Any help would be greatly appreciated!


Solution

  • Thanks for all of your helps, finally a solution is found:

    n=1;
    pt1=[data(n,1), data(n,2)];
    figure
    scatter(data(:,1),data(:,2))
    hold on
    for i=1:size(data,1)
        if isempty(pt1)~=1
            [Liaa,Locbb] = ismember(pt1(:)',data,'rows');
                 if Locbb~=0
                    data(Locbb,:)=[];
                    [n,d] = knnsearch(data,pt1(:)','k',1);
                    x=[pt1(1,1) data(n,1)];
                    y=[pt1(1,2) data(n,2)];
                    pt1=[data(n,1), data(n,2)];
                    plot(x,y);
                 end
        end
    end
    hold off
    

    enter image description here

    BTW it is possible to delete the last longest line as it is not related to the question, if someone need it please let me know.