How do I extract data from polyshape figures

8 views (last 30 days)
Cye
Cye on 10 Oct 2018
Commented: Steven Lord on 10 Oct 2018
I know how to extract data from a regular figure, e.g., open('example.fig'); a = get(gca,'Children'); xdata = get(a, 'XData'); ydata = get(a, 'YData');
However, I'm stuck when it comes to figures that were created with polyshapes.

Answers (1)

jonas
jonas on 10 Oct 2018
Edited: jonas on 10 Oct 2018
If you are looking to extract the polyshape, then it's stored in the polygon object's shape property.
So for example
% Plot polygon
p=polyshape([0 1 1 0],[0 0 1 1])
plot(p)
% Extract
ax=gca;
ax.Children.Shape
  1 Comment
Steven Lord
Steven Lord on 10 Oct 2018
I would use findobj instead of assuming that gca still refers to the axes in which the polyshape was plotted and that the axes only has one child. [For purposes of this example I do make the assumption that only one polygon was plotted, so if we find one it's the right one, but you could test that poly1 isscalar and handle the cases where it is not appropriately.]
>> p=polyshape([0 1 1 0],[0 0 1 1]);
>> plot(p);
>> poly1 = findobj(groot, 'type', 'polygon');
>> p2 = poly1.Shape;
>> isequal(p, p2)
ans =
logical
1
As written this code will find all polygon objects you've plotted because it looks for everything under groot. You can change that first input to search in just a particular figure or axes.

Sign in to comment.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!