adding extra information in datacursormode text box

Hello,
I have a subplot with hundreds of (x,y)-points displayed and I want to be able to click at a point and see some textual identification information besides the x and y values. Each point has a unique information.
The straight forward way would be to store the information in an array of same size as the data array, but what is the easiest way to access this array with the datacursormode? The datacursormode returns the x and y values of each point, but it is a little bit inconvient to use those for looking up the identification text for each point, because some points may have same values in different subplots etc.

 采纳的回答

If you right click on the data cursor text, you can choose: Edit Text Update Function.
In the function, put whatever you want.
EDIT
There are probably many ways to do what you are asking. Here is what I did, as an example.
L = plot(rand(1,4),rand(1,4),'*','markersize',8);
axis([0 1 0 1])
set(L,'userdata',{'First','Second','Third','Fourth'}) % The extra info.
Now I select the datacursor and set the text update function to this:
function output_txt = myfunction(obj,event_obj)
pos = get(event_obj,'Position');
D = get(event_obj.Target,'Userdata'); % Get the extra information.
xd = get(event_obj.Target,'Xdata');
I = find(xd==event_obj.Position(1),1);
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)],...
D{I}}; % Place extra information at end of string.

更多回答(1 个)

Is there a way to set the datacursor update function to return the array index value instead of the x-value of that cell? This would work well.

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by