Doug's MATLAB Video Tutorials

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

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.

16 CommentsOldest to Newest

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?

>> h = datacursormode

h =

graphics.datacursormanager

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

Looks like no to me.
h = plot([1 2 1])

dcm_obj = datacursormode(gcf);
set(dcm_obj,'enable','on')
set(dcm_obj,'UpdateFcn',{@NewCallback,'foo'})

-----------
function output_txt = NewCallback(obj,event_obj,str)

%str will be 'foo'

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?

figure()
plot(x,y)
dcm_obj = datacursormode(gcf)
set(dcm_obj,’UpdateFcn’,{@CustomDataTipAndPlot,’foo’})

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

Jeremy,

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.

Doug

Hi,

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.

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.

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.
Thanks.
Amnon, M.Sc student of chemistry from Israel

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.

Harold,

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

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,

Giaime,

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.

Doug

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.

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?

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

Add A Comment

What is 5 + 9?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.