I'm allowing the user to create a series of line ROIs like so, using a GUIDE app pushbutton:
function pushbuttonline_Callback(hObject, eventdata, handles)
% hObject handle to linebtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = drawline(handles.axes1);
But how can I access these later on in the code?
I was thinking of placing the drawline variables in a global array, but is there a better way of doing this?
Also, if the user deleted a line using the GUI, would the line variable also be removed?
Thanks in advance.

 채택된 답변

Adam Danz
Adam Danz 2019년 11월 22일
편집: Adam Danz 2019년 11월 22일

0 개 추천

This demo works outside of your GUI callback but it will be easy to implement it into your GUI. See inline comments for details.
% Create axes (your gui will already have axes)
handles.axes1 = axes();
% Draw 3 lines (your gui will only have 1 of these lines
% but it's important to add the 'Tag' to your line.
% The tag name should be something unique - I chose 'userLine'.
h = drawline(handles.axes1,'Tag','userLine');
h = drawline(handles.axes1,'Tag','userLine');
h = drawline(handles.axes1,'Tag','userLine');
% Later on from any other callback function that has
% access to 'handles', you can find all objects in
% your axes that has that tag. If 'lineHandles' is
% empty, no objects were found with that tag. Otherwise
% 'lineHandles' will contain all of the handles to your
% ROI lines.
lineHandles = findobj(handles.axes1,'Tag','userLine')

댓글 수: 6

John D
John D 2019년 11월 23일
편집: John D 2019년 11월 23일
If I wanted to access the axes through a function that wasn't a callback (i.e. didn't have the 'handles' parameter), how would I go about doing that? For example, the allevents function:
function linebtn_Callback(hObject, eventdata, handles)
% hObject handle to linebtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = drawline(handles.axes1,'Tag','userLine');
addlistener(h,'ROIMoved',@allevents);
function allevents(src,evt)
evname = evt.EventName;
switch(evname)
case{'ROIMoved'}
% here ------------
end
Thanks.
The only handle you really need is the axis handle handles.axes1
I don't know what is calling the allevents function so I can't guess what the src handle is.
A common way to find an axes is to use findobj(). For example, let's say you press a button that triggers allevents(). The parent of the button is the main GUI figure and your axes is also a child of the main GUI figure. If the button handle is 'h' you could find the axes using
ax = findobj(h.Parent,'type','axes')
assuming the GUI only has 1 axes (otherwise all axes handles would be returned).
Ok, I see. I suppose, to combat the error of having multiple axes, you could do this?
ax = findobj(h.Parent,'Tag','axes1')
Adam Danz
Adam Danz 2019년 11월 23일
Yes, as long as you've assigned "axes1" to the tag property of that axes!
Adam Danz
Adam Danz 2019년 11월 23일
편집: Adam Danz 2019년 11월 23일
Better yet,
ax = findobj(h.Parent,'Type','axes','Tag','axes1')
When using findobj() or findall() you want to be as specific as possible.
John D
John D 2019년 11월 23일
Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 11월 22일

댓글:

2019년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by