MATLAB Answers

How do I display custom text for the data cursor?

97 views (last 30 days)
I want to customize the text that is displayed in the text box when I use the data cursor tool.
I want to use my own labels instead of the standard X and Y coordinates that are being displayed. How do I do this? 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Dec 2016
For doing this, you need to make use of the "Edit Text Update Function" from the Data Cursor Tool context menu. 
Here is a detailed example that walks you through the different steps involved. 
1. Create a simple scatter plot as follows : 
>> scatter(rand(1,10), rand(1,10));
2. After the plot opens, click on the "Data Cursor" tool. 
3. Right click anywhere inside the plot, and select "Edit Text Update Function".
4. Edit the code in such a way that the "output_txt" variable is the label you want to display next to the selected point. This example function displays "High Risk" if both "x" and "y" values are greater than 0.5, and "Low Risk" otherwise :
function output_txt = display_risk(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).
% Get the position of the point being clicked on
pos = get(event_obj,'Position');
% If both X and Y values are greater than 0.5, label it as high risk
% Else label it as low risk
if pos(1) > 0.5 && pos(2) > 0.5
output_txt = {'High Risk'};
output_txt = {'Low Risk'};
5. Save the file with the appropriate name. In this case "display_risk.m"
6. The "Data Cursor" should now display the custom labels according to the point being selected.
7. To use the same functionality for another plot, select the "Data Cursor" tool, right-click and select "Select Text Update Function", and choose the appropriate function you want to use. 


Sign in to comment.

More Answers (1)

Giuseppe Naselli
Giuseppe Naselli on 28 Mar 2018
Edited: Giuseppe Naselli on 28 Mar 2018
Hi, thanks for this but the explanation is a bit vague.
  • Could you please explain a bit better where the display_risk.m needs to be saved?
  • And if we want to include it in a stand-alone function that is shared with others?
  • is there a way of making the display_risk function to read data from the workspace?
  • In other words, suppose I want to create a function that has x,y,z as inputs where x = coordinate x,y = coordinate y,z = text to be displayed when the cursor clicks on the corresponding point
Thanks in advance G

  1 Comment

Steven Lord
Steven Lord on 28 Mar 2018
The third example on the documentation page for the datacursormode function shows how to set a custom data cursor update function programmatically. Since you say you want to "include it in a stand-alone function that is shared with others" that may be easier for your application.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!