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)')
datacursormode on
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn)
disp('Click line to display a data tip, then press "Return"')
pause
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)
pos = get(event_obj, 'Position');
output_txt = {['x: ' num2str(pos(1))], ['y: ' num2str(pos(2))]};
end
Best Answer