Save points or data in right order

6 views (last 30 days)
For the below image,the initial plot is as follows
After doing set(gca,'YDir','reverse'); I am able to correct the display to get this
Now what I would like to know is there a way to store the points after reversing the Y Direction
  1 Comment
sparsh garg
sparsh garg on 1 Sep 2021
Edited: Walter Roberson on 1 Sep 2021
so I read that we can obtain x and y using h.xData and h.yData
So ,this is what I did
plot(pts(1,:),pts(2,:));
set(gca,'YDir','reverse');
ax=gca;
h = findobj(gca,'Type','line');
x = h.XData;
y = h.YData;
Final_set_pts=[x;y];
however when I plot final_set_pts,the result is same as before,any suggestions on what i am doing wrong here will be welcome.

Sign in to comment.

Accepted Answer

Wan Ji
Wan Ji on 1 Sep 2021
Edited: Wan Ji on 1 Sep 2021
I have answered this question before, now I have a better solution to this problem which can identify many connected regions
clc;clear
load('pts.mat');
pointSet = X_pts;
figure(1)
clf; hold on
scatter(pointSet(:,1),pointSet(:,2),40,'filled','k')
n=1;
while ~isempty(pointSet)
circleSetInd=1;
for j=1:length(pointSet)
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
[~,ind]=sort(disSet);
ind=ind(1:5);
[~,~,t_ind]=intersect(circleSetInd,ind);
ind(t_ind)=[];
if ~isempty(ind)
circleSetInd=[circleSetInd;ind(1)];
else
n=n+1;
circleSet{n}=pointSet(circleSetInd,:);
pointSet(circleSetInd,:)=[];
break
end
end
end
q = cellfun(@isempty,circleSet);
circleSet = circleSet(~q);
for i=1:numel(circleSet)
plot(circleSet{i}(:,1),circleSet{i}(:,2),'LineWidth',2)
hold on
end
set(gca,'YDir','reverse');
Or you can set some more connected regions
clc;clear
t = linspace(0,2*pi,101)';
x = 10*cos(t);
y = 10*sin(t).*cos(2*t);
pointSet = [x, y]; % this point set is generated for test
figure(1)
clf; hold on
scatter(pointSet(:,1),pointSet(:,2),40,'filled','k')
n=1;
while ~isempty(pointSet)
circleSetInd=1;
for j=1:length(pointSet)
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
[~,ind]=sort(disSet);
ind=ind(1:5);
[~,~,t_ind]=intersect(circleSetInd,ind);
ind(t_ind)=[];
if ~isempty(ind)
circleSetInd=[circleSetInd;ind(1)];
else
n=n+1;
circleSet{n}=pointSet(circleSetInd,:);
pointSet(circleSetInd,:)=[];
break
end
end
end
q = cellfun(@isempty,circleSet);
circleSet = circleSet(~q);
for i=1:numel(circleSet)
plot(circleSet{i}(:,1),circleSet{i}(:,2),'LineWidth',2)
hold on
end
set(gca,'YDir','reverse');
  2 Comments
sparsh garg
sparsh garg on 1 Sep 2021
thanks for the discussion.
Two things ,
the new code is still not generalizing and fails on the foll test case
Moreover,for the current question what I want is that when we do set(gca,'YDir','reverse),the resultant points that show up on the figure,that is the correct order I want.
So in order to do that what can I do,I have tried rotating the original set of reordered points(from your code) by a rotation matrix but that doesn't work,
Wan Ji
Wan Ji on 1 Sep 2021
Well, this is not a problem, you just need a photo size to do this then
plot(pts(1,:),max(pts(2,:))+1-pts(2,:));
% set(gca,'YDir','reverse');
ax=gca;
h = findobj(gca,'Type','line');
x = h.XData;
y = h.YData;
Final_set_pts=[x;y];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!