Trace along specific direction of perimeter of plotted object

Consider the following gif:
As you can see, when I loop through the [x y] data, it doesn't pick a direction to travel around the object. How can I make my loop such that it travels a specific direction and returns to where it began? I thought about sorting the data, that didn't work though.
Thanks in advance!

Answers (2)

Alright I figured it out. For any future people who has this problem use bwtraceboundary. It will return a matrix with the correctly sorted data to loop around the object.
For gif, it appears that your data is something like this
x = [x1 x1 x2 x2 x3 x3 x4 x4 ....... xn xn]; % every element is repeated twice
y = [y1 y2 y3 y4 y5 y6 y7 y8 .......... yn]; $ y1 y3 y5 to lower line, y2 y4 y6 to upper line
you just need to rearrange your vectors
xNew = zeros(1, length(x));
yNew = zeros(1, length(y));
[~, index] = unique(x);
xNew(1:length(index)) = x(1:2:end);
x(1:2:end) = [];
xNew(length(index)+1:end) = x(end:-1:1);
yNew(1:length(index)) = y(1:2:end);
y(1:2:end) = [];
yNew(length(index)+1:end) = y(end:-1:1);
Try running this and plot using xNew and yNew.

6 Comments

Thank you for the response. I can't run it since t is not defined. What should t be?
Oh! I was testing it before posting here and forgot to edit t, now check the edited code.
I get the error
In an assignment A(:) = B, the number of elements in A and B must be
the same.
For the line
xNew(1:length(index)) = x(1:2:end);
I tried to check the size of the left and right side of the equal sign. The left side has size
1 94
and the right side has size
149 1
Maybe it's because every element of x can be repeated more than two times?
Can you show you x and y vector. It appears that they don't follow this pattern
x = [x1 x1 x2 x2 x3 x3 x4 x4 ....... xn xn]; % every element is repeated twice
y = [y1 y2 y3 y4 y5 y6 y7 y8 .......... yn]; $ y1 y3 y5 to lower line, y2 y4 y6 to upper line
Thank you for the patience. I have attached the two coordinate vectors.
I figured it out, thank you for the help.

Sign in to comment.

Categories

Asked:

on 27 Apr 2018

Commented:

on 27 Apr 2018

Community Treasure Hunt

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

Start Hunting!