필터 지우기
필터 지우기

Editing impoly polygons in a GUIDE app

조회 수: 5 (최근 30일)
RonE
RonE 2019년 8월 23일
댓글: Adam Danz 2019년 8월 26일
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
RonE
RonE 2019년 8월 23일
편집: RonE 2019년 8월 23일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 8월 23일
"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
RonE
RonE 2019년 8월 26일
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 2019년 8월 26일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by