how to extract cursor data to a variable ?

26 Ansichten (letzte 30 Tage)
Hugo Trentesaux
Hugo Trentesaux am 5 Jul. 2018
Kommentiert: Ganzorig am 12 Mär. 2025
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).

Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 10 Jul. 2018
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 Kommentare
Rachid Belaroussi
Rachid Belaroussi am 4 Mär. 2021
Bearbeitet: Rachid Belaroussi am 4 Mär. 2021
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 am 12 Mär. 2025
Thanks,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by