How to add additional info to the data cursor?

Hello
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.
Lukas
(sorry for the self-bumping)

 Accepted Answer

ChristianW
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);
plot(X,Y)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,t})
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))]};

7 Comments

To follow up, for my own use I have a custom datacursor function to display the Index as well. Hopefully this will help you Lukas:
function output_txt = datacursor(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
idx = get(event_obj, 'DataIndex');
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)] };
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
output_txt{end+1} = ['Index: ', num2str(idx)];
end
To use it, define a datacursormode object and set the 'UpdateFcn' callback to your custom function, e.g.,
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @datacursor)
Lukas
Lukas on 25 Mar 2013
Edited: Lukas on 25 Mar 2013
Ok, thanks, this is already very useful, it's much easier to have the index instead of having to find it manually. But is there a possibility to then use this index in another vector to have this additional info (stored at that index in the other vector) displayed on the datacursor? Probably I should give this vector to the datacursor function, but I don't think set will be able to handle it then...
That works. Exactly what I was looking for. Thank you very much.
I = get(event_obj, 'DataIndex')
is always 1 for me on MATLAB 2018b - therefore the correct metadata cannot be found.
lifesaver! Works perfectly, thank you!
what is event object and obj ? is it a data we load?

Sign in to comment.

More Answers (1)

Hello .. Good night. Is there any method, where I click on a certain point in the figure, and I want to extract the data from the cursor, and add it to a new program in PROMPT?

Categories

Asked:

on 21 Mar 2013

Commented:

on 20 Sep 2023

Community Treasure Hunt

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

Start Hunting!