The matlab doc gives a way to manually Export Cursor Data to Workspace. Is there a function to do this automatically ? I would like to make computations on the cursor coordinates that the user drags in the plot (imshow).

 채택된 답변

Kojiro Saito
Kojiro Saito 2018년 7월 10일

7 개 추천

datacursormode and getCursorInfo will be a solution.
x = linspace(0,10,150);
y = cos(5*x);
fig = figure;
plot(x,y,'Color',[0,0.7,0.9])
title('2-D Line Plot')
xlabel('x')
ylabel('cos(5x)')
% Enable data cursor mode
datacursormode on
dcm_obj = datacursormode(fig);
% Set update function
set(dcm_obj,'UpdateFcn',@myupdatefcn)
% Wait while the user to click
disp('Click line to display a data tip, then press "Return"')
pause
% Export cursor to workspace
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
disp('Clicked positioin is')
disp(info_struct.Position)
end
function output_txt = myupdatefcn(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
pos = get(event_obj, 'Position');
output_txt = {['x: ' num2str(pos(1))], ['y: ' num2str(pos(2))]};
end

댓글 수: 7

paul harder
paul harder 2019년 5월 1일
thank you
Jerry
Jerry 2020년 4월 19일
How is this possible with multiple data tips? When I try it with more than one data tip the error code
'Error using dispToo many input arguments.' appears.
Heini Rasmussen
Heini Rasmussen 2020년 5월 29일
@Jerry: Did you find a solution to this?
info_struct(1).DataIndex
info_struct(2).DataIndex
.......
Angie Vicente
Angie Vicente 2021년 1월 3일
help! how i can extract two datas with this code?
Rachid Belaroussi
Rachid Belaroussi 2021년 3월 4일
편집: Rachid Belaroussi 2021년 3월 4일
tx kojiro, that help so much! I might add (tx to francesco): multiple data tips can be done by alt+left click to add a new point
Ganzorig
Ganzorig 2025년 3월 12일
Thanks,

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

추가 답변 (0개)

카테고리

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

질문:

2018년 7월 5일

댓글:

2025년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by