MATLAB Answers

How to add additional info to the data cursor?

297 views (last 30 days)
Lukas on 21 Mar 2013
Commented: Julia Jose on 18 Feb 2020
I'm wondering how I can get extra info (which is not plotted itself) to show on the data cursor of a plot.
Say I have multiple events, and three variables per event (nicely stashed in arrays), e.g. the time of the event, the position and some measured value. Say now that I plot this measured value against the position. Is there some way to also have the time of the event displayed in the data cursor per event?
Thanks in advance.
(sorry for the self-bumping)


Sign in to comment.

Accepted Answer

ChristianW on 22 Mar 2013
Edited: ChristianW on 26 Mar 2013
function test_main
% Plots graph and sets up a custom data tip update function
fig = figure('DeleteFcn','doc datacursormode');
X = 0:60;
t = (X)*0.02;
Y = sin(-16*t);
dcm_obj = datacursormode(fig);
function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['I: ',num2str(I)],...
['T: ',num2str(t(I))]};


Show 3 older comments
Lukas on 26 Mar 2013
That works. Exactly what I was looking for. Thank you very much.
Nicolai Spohrer
Nicolai Spohrer on 17 Feb 2020
I = get(event_obj, 'DataIndex')
is always 1 for me on MATLAB 2018b - therefore the correct metadata cannot be found.
Julia Jose
Julia Jose on 18 Feb 2020
lifesaver! Works perfectly, thank you!

Sign in to comment.

More Answers (0)