File Exchange

image thumbnail

Programmatically Datatip Generation with Matlab Implementation

version (2.27 KB) by Hristo Zhivomirov
Programmatically generation of a custom made datatip on a given Matlab figure.


Updated 28 Dec 2017

View Version History

View License

The present code is a Matlab function that provides programmatically generation of a custom made datatip on a given Matlab figure (e.g. 2D and 3D lines, 3D surfaces, etc.). The function is very suitable for dynamic (animated) plots. The user can:
1) set the datatip position anywhere on the plot via data indexes;
2) set a custom X, Y and Z datatip labels;
3) hold the previously placed datatips.
Two examples are given in order to clarify the usage of the function. For convenience, the input and output arguments are given in the beginning of the function.

Warning: One must uncomment the code line 55 for Matlab R2014a and earlier.

Cite As

Hristo Zhivomirov (2021). Programmatically Datatip Generation with Matlab Implementation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Siegfried Martius

Hello Cherelle,
with these commands you can change the some properties of datatips

set(d_1,'FontWeight', 'normal')
set(d_1,'TextColor', 'b')
set(d_1,'Orientation', 'bottomright') % top: left,right bottom: left,right
good luck

Siegfried Martius

Hello Cherelle,
with the problem below I have asked the Matlab support. Here is the answer from Catherine

Hello Siegfried,

My name is Catherine, and I am writing in reference to your Technical Support Case #04597746 regarding 'Datatips on different plots in one figure'.

I understand that the issue you are encountering is that you would like different datatips to have different labels, but they all end up with the same label when using the 'datatip.m' file. The behavior you are seeing is the result of a change made in MATLAB R2019a, in which setting a datacursormode's 'UpdateFcn' will cause all existing data tips to be updated to use the specified UpdateFcn (your current version of 'datatip.m' file should give the intended results if run in an earlier version of MATLAB). In order to obtain the correct results in MATLAB R2020b, there are some changes that must be made to 'datatip.m' as well as the format of the datatip labels that are passed in. You can put all of the labels in a cell array, and then indicate which indices correspond to which datatip. I am attaching two files, 'datatipPlotEx.m', which includes a simple example of creating different datatip labels on different curves on the same plot, as well as an updated version of 'datatip.m'. You can see how to set up the datatip labels based on lines 4-6 of 'datatipPlotEx.m'.

I hope this helps; please let me know if you have any follow up questions and I will be happy to assist.

Catherine Matulis
MathWorks Technical Support Department

and the example

h = plot(1:3);
hold on;
h2 = plot(5:10);
str1 = {'1st x value: ', '1st y value: '};
str2 = {'2nd x value: ', '2nd y value: '};
allStr = {str1, str2};
datatip(h, 2, allStr, 1, 'hold');
datatip(h2, 2, allStr, 2, 'hold');

Now all my datatips are right.
Kind regards ans stay healthy

Cherelle Connor

I am having a similar issue to that mentioned by @Siegfried Martius below. Has anyone happened to find a solution? I am using Matlab 2020b.

Siegfried Martius

I plot two curves in one figure with different plot handles. So I want to make a datatip at curve one with a special string text one
(for example {'Frequenz/GHz=','|\rho_B|^2/dB='})
and an other datatip at the curve two with an other string text
(for example {'Frequenz/GHz=','|\tau_B|^2/dB='})
If I use your program datatip twice(the seond with the 'hold' option), the first string were overwritten by the last one.
So both datatips have the same text, but the true x,y values.
Do you see a possibility to overcome this problem.
Kind regards Siegfried



Hello, great function here. Just wanted to say that in order to prevent the text in all datatips from changing to match the newest str, I commented out line 55 (updateDataCursors(cursorMode)). This allowed me to put many different data tips with different text in each on the same plot. Not sure if this was the right way to do it, but it works quite well! Thank you for creating this function.

Hristo Zhivomirov

Hi Siddaharth! First of all, thank you for your interest in my submission! As I wrote in the file information form, please comment the function code line 53 for Matlab R2014b and later, this will solve the problem.

All best,
Hristo Zhivomirov

Siddharth Parmar

Hi there,
when I run the examples 1 and 2 I get an error in line 53.
Could you look into it?

Hristo Zhivomirov

Hi Philipp! My function use a matrix indexing and the indexes must be integers. If your main program pass the noninteger indexes to the function, you must round them beforehend.

Best regards,
Hristo Zhivomirov



thanks for this submission,
does a way exist for creating datatips at non-integer indices?
That option corresponds to the datacursormode object's property 'SnapToDataVertex', which may be switched off to allow for non-integer indices.

Kind regards,

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

Inspired by: MAKEDATATIP

Community Treasure Hunt

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

Start Hunting!