MATLAB: How does getPosition work in impoly

Image Processing Toolboximpoly

Hello,
i've been using impoly for drawing polygon in a blank figure, and then use the pos=getPosition(h) function to get the position of the drawn polygon. but the problem is, when i changed the corner of the drawn polygon, the "h=getPosition" function won't retrieve (update) the new position, is there a way to update the position while we change the corner?.
also, suppose that i draw a few polygon in a figure using only "h=impoly" then i want to delete one of the previous polygons (by choosing one of the drawn polygons to be deleted). i have used the "delete(h)" command but only the previous body that can be deleted.
thanks in advance 🙂

Best Answer

I'm not seeing the behavior you observe. You wrote the "h=getPosition" function, so I'm wondering if you're overwriting your h variable (so it no longer refers to the impoly object)? Or maybe that you have multiple ones (given your second question) and you're changing one, but that's not the one your h variable refers to.
Anyway, to address both questions:
imshow('street1.jpg')
h(1) = impoly(gca,[20,20;80,20;80,80;20,80])
h(2) = impoly(gca,[200,200;280,200;280,280;200,280]);
x1 = getPosition(h(1))
% Move one of the corners of the first polygon
x2 = getPosition(h(1))
x1 - x2
Note that h is a 2-element impoly object (1 element per shape). x1 and x2 are numeric arrays of the coordinates.
EDIT TO ADD: OK, didn't understand the (first) question at all, sorry! The answer to the second part stands: just make h an array of impoly objects (or, of course, just have multiple objects with different variable names, but that's harder to maintain).
For the first part, about updating xy whenever you move a vertex, how about this:
addNewPositionCallback(h(1),@(p) assignin('base','xy',p));
This is dangerous, in that you are clobbering anything in the base workspace with the name xy. And if you want this in a function, then don't use the base workspace.
Also, I just realized I didn't fully answer the second part, about deleting. The point I was making was that if you keep all the handles in an array, you can delete any of them:
delete(h(1))