Editing impoly polygons in a GUIDE app

5 views (last 30 days)
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
  1 Comment
RonE
RonE on 23 Aug 2019
Edited: RonE on 23 Aug 2019
I have a function that is called when a user clicks a button on the GUI:
function handles = draw_polygon(handles)
h = impoly; %allow user to draw polygon
fcn = @posChanged;
addNewPositionCallback(h, fcn);
points = h.getPosition;
...
end
function posChanged(pos)
pos %write out for testing
end
posChanged gets called when I move vertices of the polygon, but I cannot save the changes because I do not have access to handles in posChanged. Is there a way to pass handles into posChanged? handles is essentially a place where I store "global" variables in the GUI app.
Ron

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Aug 2019
"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.
  14 Comments
RonE
RonE on 26 Aug 2019
It looks like I have it working pretty well, although I need to redraw the frame when the user stops editing (dragging a vertex, etc). Thanks for your help. I don't know how to detect when the user stops editing. Maybe I would have to use a timer. You have a any idea?
Adam Danz
Adam Danz on 26 Aug 2019
Maybe you could add a button to your figure. After the fig is created, this line below will add a button to the bottom, left corner.
uicontrol('style','pushbutton','Units','normalize','position',[.05,.05,.08,.05],'String','Done','Callback',@yourfunc)
You'll need to write a sensible callback function that sets some kind of flag to signal the editing is done to execute the next steps.

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!