display specific name on a graph with the cursor

1 Ansicht (letzte 30 Tage)
Christophe Nell
Christophe Nell am 31 Jul. 2015
Kommentiert: Christophe Nell am 4 Aug. 2015
Hello, I want to be able to display the name of the dot in a graph according to the vector position.
my code is
function
fig = figure;
a = [1 2 3 4 5]
b = [2 3 4 5 6]
name{1} ='one';
name{2} ='two';
name{3}='tree';
name{4}='four';
name{5}='five';
scatter(a, b, 'filled')
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn)
end
function txt = myupdatefcn(empt,event_obj)
pos = get(event_obj,'Position');
nameNumber = 'WantCorrectNameHere';
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
nameNumber};
end
So I want to have access to the right name at the selected position for exemple for a(1);b(1) -> name{1}

Akzeptierte Antwort

Jan
Jan am 31 Jul. 2015
Bearbeitet: Jan am 31 Jul. 2015
Perhaps the SnapToDataVertex mode is useful?
function
fig = figure;
a = [1 2 3 4 5];
b = [2 3 4 5 6];
name = {'one', 'two', 'three', 'four', 'five'};
DCM_Data.Position = [a; b];
DCM_Data.Title = name;
scatter(a, b, 'filled')
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn', {@myupdatefcn, DCM_Data});
end
function txt = myupdatefcn(empt,event_obj, DCM_Data)
pos = get(event_obj,'Position');
[dummy, index] = min(((pos(1) - DCM_Data.Position(1, :)).^2) + ...
((pos(2) - DCM_Data.Position(2, :)).^2));
nameNumber = DCM_Data.Title{index};
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
nameNumber};
end
  2 Kommentare
Christophe Nell
Christophe Nell am 4 Aug. 2015
Ok, thank you. I used a for loop and checked the positions of my objects compared to the cursor but this strategy is more elegant. Best
Tolga Karabiyikoglu
Tolga Karabiyikoglu am 29 Mär. 2021
Thanks, Bought me out of a trouble so cheap...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by