MATLAB: Editing impoly polygons in a GUIDE app

guideimpolyMATLABpolygons

In a GUIDE app, I want to draw polygons on an image (say 4 polygons), then copy the polygons and display on a different image, then edit those polygons and save the changes with the new image. AFAIK, I need to use impoly because I have R2017b. I click a button on the GUI, which runs a function where I use h = impoly. I am experimenting with addNewPositionCallback inside this function, but after this function has executed, I don't see any way to get to that polygon again to detect changes. When I display a particular image, I want to draw the polygons associated with that image (not a problem), but the problem comes when I want to detect edits that the user can then make to the polygons associated with that image. Any examples of doing this would be appreciated. Again, I don't have a problem storing polygons and drawing them later, the problem is editing them and saving the changes.
Thanks
Ron

Best Answer

"after this function has executed, I don't see any way to get to that polygon again to detect changes"
Once the callback function is finished, all variable values within the function are lost forever unless you save them (exceptions are global and persistant variables which are not recommended).
You can use guidata() to save variables to your GUI so when you enter a callback function, those values can be retreived. Here's a demo that stores/retreives data in/from the GUI figure itself but you could chose any GUI object.
function myCallbackFunction(hObject, event, handles)
h = impoly(. . .);
dat = guidata(handles.figure); %assuming the handle to your figure is named 'figure'
if ~isfield(dat,'PolyHandles')
% add the field if it doesn't exist already
dat.PolyHandles = [];
end
dat.PolyHandles(end+1) = h; %add new handle to end
guidata(handles.figure,dat); %store the updated data
end
To clear the handles,
dat = guidata(handles.figure);
dat.PolyHandles = [];
guidata(handles.figure,dat);
Once you have the handles, you can do everything in your question: edit them, copy them to another image, etc.