Data Cursor Position in GUI

조회 수: 14 (최근 30일)
Edward
Edward 2011년 5월 22일
편집: Mo Ba 2017년 5월 8일
I am building a GUI in GUIDE.
The user selects an Image file, which is then displayed in a separate figure/window.
The datacursormode is activated for the figure, and the datatip text is customized.
I want to retrieve the data-cursor's position through the myupdatefcn. I am able to display the position info each time the user clicks somewhere on the image, but I can neither set or setappdata the position for use in another function.
...
imshow(I);
dcm_obj = datacursormode(f);
datacursormode on;
set(dcm_obj,'UpdateFcn', @myupdatefcn )
end
function txt = myupdatefcn(~, event_obj)
pos = event_obj.Position;
disp(['You clicked X:',num2str(pos(1)),', Y:',num2str(pos(2))]);
txt = {'Point to Compute'};
end
How can I retrieve the data cursors' position for use in a separate function in my m file?
p.s. I've tried getCursorInfo(dcm_obj). I haven't been able to get it to work though, even by using a pause or waitforbuttonpress command.
p.p.s. I've read through http://www.mathworks.com/help/techdoc/ref/datacursormode.html. It hasn't helped me much.
Thanks in advance for any help.

채택된 답변

Matt Fig
Matt Fig 2011년 5월 22일
Add this line to your myupdatefcn:
set(0,'userdata',pos);
Then from any workspace you can do:
pos = get(0,'userdata');
  댓글 수: 2
Edward
Edward 2011년 5월 23일
Thank you for the response, this worked for me.
Just a small question, it seems that the user data is not cleared between runs. Unless I restart Matlab, the information is kept.
The solution that I have to this is to set this user data to an empty vector in the OpeningFcn. This works, just wondering if there is a better way, since this seems a bit hackish.
Thanks again.
Matt Fig
Matt Fig 2011년 5월 23일
The userdata is there for you to use, so I don't consider it hackish at all to do so. However, you could also store the pos in the userdata of any object which has this property. So for instance you could set the userdata of the axes object which has the datacursor.
set(gca,'userdata',pos)
Then to find the particular axes of interest (if there are more than one up at a time), each should have a unique tag. Then the data is retrieved from any workspace with something like:
pos = get(findobj('tag','myaxestag'),'userdata');
If the axes is in a GUIDE GUI, use FINDALL instead when looking from outside a callback workspace. And if there is only one, non-GUIDE axes, gca should work...
I first suggested using the root object's userdata because it is automatically accessible from anywhere without worrying about handle visibility, differentiating between multiple objects of the same type, etc.
Also, see SETAPPDATA and cousins...

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

추가 답변 (1개)

Travis
Travis 2011년 7월 11일
I am doing a very similar thing, although I am having trouble getting mine to work. I am making a GUI using GUIDE. I need to gather the position info from an axes plot on the GUI, for any number of clicks. In other words I need "pos" to become available in the workspace at any click in the "MomentCurv" axes. Any help is much appreciated!
Here is the opening function:
function Analysis_OpeningFcn(hObject, eventdata, handles, varargin)
X = ...
Y = ...
axes(handles.MomentCurv);
datacursormode on;
plot(X,Y);
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myupdatefcn);
pos = get(0,'userdata');
guidata(hObject, handles);
end
Here is the myupdatefunction:
function txt = myupdatefcn(empt,event_obj)
pos = event_obj.Position;
txt = {['Phi: ',num2str(pos(1))],['Moment: ',num2str(pos(2))]};
set(0,'userdata',pos);
end
  댓글 수: 1
Mo Ba
Mo Ba 2017년 5월 8일
편집: Mo Ba 2017년 5월 8일
Try what @Loginatorist said above. It works with no problem:
Add this line to your myupdatefcn:
set(0,'userdata',pos);
Then from any workspace you can do:
pos = get(0,'userdata');

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by