File Exchange

image thumbnail


version (2.52 KB) by Tim Farajian
Allows programmatically adding datatips to any valid graphical object.


Updated 27 Jun 2012

View License

MAKEDATATIP allows programmatically adding datatips to some graphical objects.

Updated to allow adding datatips to additional handle objects including scatter plots and images. If there are other object types you want supported that do not currently work, please let me know.

x = 1:10;
y = rand(1,10);
hPlot = plot(x,y);
makedatatip(hPlot,[3 8])

[X,Y,Z] = peaks(30);
hObj = surf(X,Y,Z);
I = [5 8; 20 12; 22 28];
hDatatip = makedatatip(hObj, I);

Cite As

Tim Farajian (2020). MAKEDATATIP (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (42)

Zhonglu Lin

Zhengyi thank you!!! This code is obsolete please look at the comment of zhengyi below for the updating

Not working with exemplary code

Ben Peng

Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for
equality, use '=='.


Thanks for your help, Zhengyi


Great script but needs update.

For 2017b, you need to make a few modifications:

1. lines 101 and 105:
hDatatip = zeors(...) -> hDatatip = cell(...)

2. lines 133 and 144
hDatatip(n)... -> hDatatip{n}...

3. line 136
set(get(hDatatip(n),'DataCursor'),'DataIndex',pos, 'TargetPoint',pos(1:2))
hDatatip{n}.Cursor.DataIndex = pos; hDatatip{n}.Cursor.Position = pos(1:2);

4. line 139
set(get(hDatatip(n),'DataCursor'),'DataIndex',index(n, :),'TargetPoint',pos)
hDatatip{n}.Cursor.DataIndex = index(n,:); hDatatip{n}.Cursor.Position = pos;

bien le

Hello! I dont find the comment of Lliya. I have this problem:
"Error using makedatatip (line 95)
Subscript indices must be positive integers."
How Can I fix it?

Anyone knows how to export the DataCursor number to the work space?

Pham Dang

Pham Dang

I have the same error on Matlab R2015b :
Error using get
There is no DataCursor property on the PointDataTip class.

Error in makedatatip (line 139)
set(get(hDatatip(n),'DataCursor'),'DataIndex',index(n, :),...

it seems the matlab objects have changed.

I replaced
set(get(hDatatip(n),'DataCursor'),'DataIndex',index(n, :),...

by :
propPointDataCursor = get(hDatatip(n),'Cursor');
propPointDataCursor.DataIndex = index(n, :);
propPointDataCursor.Position = pos;

and it works

Best regards

Hello everyone,

I get the following error, both using my code and with the example...
Any idea?? thaks!
@Guanfeng Gao (@ Lliya I don't find your answer)

Error using get
There is no DataCursor property on the PointDataTip class.

Error in makedatatip (line 139)
set(get(hDatatip(n),'DataCursor'),'DataIndex',index(n, :),...


To work with datetime variable (present in the R2016a & b Matlab, add this lines before the for loop

if isa(X, 'datetime') || isa(X, 'duration'), X=datenum(X-min(X)); X=X/max(X); end
if isa(Y, 'datetime') || isa(Y, 'duration'), Y=datenum(Y-min(Y)); Y=Y/max(Y); end
if isa(Z, 'datetime') || isa(Z, 'duration'), Z=datenum(Z-min(Z)); Z=Z/max(Z); end
catch ME

Works very smoothly! Thank you very much!

JS Jiang

Exactly what I needed.


I was wondering why there is a check for positive integers for the subscript of the datatip. The subscript depends on the value on the x-axis/y-axis and these axes could have negative as well as non integer values.

In my data for example the values on the x axis run from 0 to 0.9 and the function was failing because of that. so i modified it to not throw an error.


Hello everyone,

The file works great with the example but when i try it with my file, i get this message :
"Error using makedatatip (line 95)
Subscript indices must be positive integers."

Someone can help me ?

Thanks for your help

Jianwei Tu

Awesome, thank you.

Akash Shah

Z Chen

This is wonderful.
@Guanfeng Gao: Check the answer given by Lliya.

There shows the Error even when I use the example, could anyone tell me how to fix it?:

Error using get
There is no DataCursor property on the PointDataTip class.

Error in makedatatip (line 139)
set(get(hDatatip(n),'DataCursor'),'DataIndex',index(n, :),...

Fantastic! Just what I needed.


Make the chnages that Illiya recommends and it works just fine.



One note though, when using HG2 (the new graphics engine that was officially introduced in MATLAB 2014b), a couple of changes need to be made to the code:
***********CHANGE 1*********
line 122 needs to be:
pos = [X(index(n)) Y(index(n)) 0];
***********CHANGE 2*********
lines 135-141 should be commented OUT

* *



Thanks, quite useful.
Would be nice to also be able to pass an axes/figure handle, which causes the tip to be created for the first graphics object (patch, surface..) found.


This is a very useful, well-written function. His help is complete with many examples. I also like how he has extensive error-checking in the code, to ensure proper usage.

I have an image and I need to put datatips on pixels in positions [61,98] and [63,94] then I try to use this function calling my image J:
hObj=imshow(J);makedatatip(hObj,[61,98; 63,94])
but I got an error:
??? Error using ==> get
There is no 'ZData' property in the 'image' class.

Then I commented line 65 and write
Z=[]; the truth is I am only going to used for images. But then I have a second error.
??? Attempted to access X(61); index out of bounds because numel(X)=2.

Error in ==> makedatatip at 99
pos = [X(index(n)) Y(index(n))];
Can somebody help me.



very good, well coded and useful function.
Just a note. I use R2008a and tried to use it on a "imagesc" graphical object. The function doesn't work since image properties have different XData and YData and no Zdata. I had to tweak a little the code.

James Ross

Any chance of posting this using the BSD License?


Is there an easy way to allow makedatatip work with scatter plots?

??? Error using ==> makedatatip at 57
Objects of class 'specgraph.scattergroup' are not a valid targets for


Yannick T

Very useful indeed. I don't know why Matlab itself doesn't provide this feature, I spent a lot of time searching the documentation for a way to do this.

Daphne W

Just what I was looking for on the Newsgroup a few months ago. Works perfectly on 2006b as well.

Ben Kang

Very useful! Sometimes, it's hard to find and select exactly the point, especially for 3-D complex surface.

John D'Errico

A nice idea. Good help, lots of examples, good error checking, an H1 line. Well done.


Included a BSD license.
Updated to allow additional objects including scatter plots and images.

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux