MATLAB: How to display custom text for the data cursor

annotation;brushcaptioncustomdatadisplayeditlabel;MATLABmousemouseoveroverpaintpaintbrushtext;textbox;

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? 

Best Answer

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'};
else
output_txt = {'Low Risk'};
end
====
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.