Stuart’s MATLAB Videos

Watch and Learn

Tutorial: How to make a custom data tip in MATLAB 21

Posted by Doug Hull,

If you use the standard data tip in MATLAB, it will annotate the X and Y value of a data point. This video will show how to customize the information that is shown in that data tip.

21 CommentsOldest to Newest

Dan Dolan replied on : 1 of 21

Great tip Doug, but is there a way to override the default data cursor update function? I see how you could create a custom figure function (my_figure) with a special data cursor, but is there root property like ‘DefaultDataCursorUpdateFcn’ that covers all figures?

dhull replied on : 2 of 21
>> h = datacursormode

h =


>> set(0,'defaultDataCursorManagerUpdateFcn',@sin)
Error using set
Can't resolve object type in default property

Looks like no to me.
dhull replied on : 3 of 21
h = plot([1 2 1])

dcm_obj = datacursormode(gcf);

function output_txt = NewCallback(obj,event_obj,str)

%str will be 'foo'

Jeremy H replied on : 4 of 21

I’m would like my update function call another function that plots a corresponding plot to that data point. After I do this the datacursor changes when I hover over different values on the original plot rather than forcing me to click on those points. This is bad because every time I hover over a new point my custom data tip function is called which creates another new plot. Is there a way to not cause it to do this?

dcm_obj = datacursormode(gcf)

function output_txt = CustomDataTipAndPlot(obj,event_obj,str)
output_txt = ‘balls.wishthisworked’;

doug replied on : 5 of 21


I have seen weirdness in this kind of thing before. I think GCA and GCF get confused in these callbacks. I think you will find lots of problems in this area, and am not sure without specifics of your case what to recommend.


Felipe replied on : 6 of 21


How can I use this in a multiple line plot, so I can hover over a line and see it’s data source (different data sources for each line)?

thanks for the tutorial.

doug replied on : 7 of 21

I would make the callback function look at some aspect of the line, like userdata or tag, and modify the display based on that. You would need to modify the userdata or tag appropriatly when you create those objects.

Amnon replied on : 8 of 21

Hi Doug,
I’m making very good use of your videos. Thanks.

I’m trying to edit my own dataTip that came from GUI (another video). Matlab says
“The name ‘Position’ is not an accessible property for an instance of class ‘uitoggletool’.”
I don’t understand how to approach it.
Amnon, M.Sc student of chemistry from Israel

Harold replied on : 10 of 21

Is there a way to edit the data in the datatip tool. For instance, I want to be able to click a plotted point and then change the location of the point by editing the values in the datatip.

Doug replied on : 11 of 21


Sorry, there is not a good way to do that with the Data Tip infrastructure. You would end up having to write almost all of that yourself. Doug

Giaime Origgi replied on : 12 of 21

Hi Doug,using a Callback for ‘UpdateFcn’, is it possible not only to change the data tip as explained in the example
“doc_datacursormode.m”, but even to do some action?

For Ex, if i have an array of struct in the workspace like A.field1 (where field1 is a 1×2 matrix), is it possible to look at the Position of the current cursor and search in in the array of structure if there is the same coordinate?

I tried with different solutions but it seems not to work in the way i’ve used..

Thanks for your help and best regards,

Doug replied on : 13 of 21


You can put whatever code you like in the function. Beware of what variables are in and out of scope and the order things are called, but yes, you should be able to do what you are talking about.


Mat replied on : 14 of 21

Hey Doug!
Thank your very much for your input. I’m struggling with following problem: I’d like to display an additional variable in the Data tip, but I couldn’t figure out how to pass it properly to the display-tip function. The problem seems to be that ‘myfunction’ doesn’t know the Variable I’d like to display, hence it must be passed.

I would appreciate it very much to receive a method to approach this problem.

Jon replied on : 15 of 21

Hi Doug,

Sorry to resurrect an old thread. Thanks for the nice tutorial. I’m trying to do a slightly more involved version of the above and got stuck on one detail.

Suppose the plot has multiple objects (for example, say we have two lineseries with different colors). I’d like to have the custom datatip show up for only some of those objects, but not others.

It’s easy enough to query the properties of event_obj from within the callback (@myfunction in your example) to figure out whether or not the selected object is one for which I want to have a datatip (for example, I could query the color or MarkerSize). But I notice that even if I output_txt = [ ] and don’t do anything else within the callback, I still get a datatip box.

My question is, how would I prevent that from happening inside the callback, or delete it after it does? In other words, how can I reliably find the handle of the datatip that gets created as soon as the callback is fired, so that I can delete it within that test block if I detect a click on an object that shouldn’t be labeled?

Uday Shankar replied on : 16 of 21

Hi Doug,
I can’t seem to make the tooltip a TeX string. Is there a way to enable TeX mode?

kiumars replied on : 18 of 21

Hello, I want to know how put a “datatip” in specific position in a plot figure. thanks

Stuart McGarrity replied on : 19 of 21

Are you looking to display information on a data point or just at a random location on the axes? Data tips are designed to give more information about a data point. If you just want to display text on a graph you can try the “text” function.

Sanwal Yousaf replied on : 20 of 21

Can i use this method to create data tips for outliers in a box plot. I want to relabel the values displayed when i click the outlierss, and use one of the values displayed to create another variable which uses this value to extract a name from a matrix that would have already been created prior to creating the box plots

Stuart McGarrity replied on : 21 of 21

You can make the data tip be a function of the X/Y values, so if you know how to identify the outliers I would think you could.

Add A Comment

What is 6 + 7?

Preview: hide