Reputation: 119
I'm trying to create my own function to display what I need when I use the data cursor in a plot. More precisely I have many plots in the same figure and I want to display a precise name for every one of these plots when the cursor is over them.
EDIT: Example: let's immagine that I have plottet 100 functions in a single figure, and these functions have some names: f1, f2, f3, ...., f100. Then when I'm watching the plot I would like to be able to see in the data cursor the name of every single one of these functions. For example if I have the mouse over the last function I would like to appear in the data cursor the string 'f100' instead of the coordinates of the point.
To do that I've seen I should use 'UpdateFcn' in this way:
dcm_obj = datacursormode(gcf);
% Set the UpdateFcn to the function myCursor
set(dcm_obj, 'UpdateFcn', @myfunction);
where myfunction is the custom function that in output_txt should give the string that I want to show:
function output_txt = myfunction(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text (string or cell array
% of strings)
In event_obj there are the Position and the Target, where the Position is an array specifying the x, y, (and z) coordinates of the cursorthe, while the Target is the handle of the graphics object containing the data point.
More informations at: http://it.mathworks.com/help/matlab/ref/datacursormode.html
I thought a possible solution: to memorize the string with the name of every plot while I'm plotting it in the handle of the graphics object containing the data point (Target), but I don't know if it's possible and, if it is, how to do that.
Is there any other solution?
Upvotes: 2
Views: 889
Reputation: 65440
Since you know the names when you call plot
, you can store the name in either the DisplayName
or UserData
fields of the plot object. You can then access these from within the UpdateFcn
callback.
As an example of DisplayName
:
plot(rand(10, 1), 'DisplayName', 'a');
plot(rand(10, 1), 'DisplayName', 'b');
plot(rand(10, 1), 'DisplayName', 'c');
function updateFcn(~, event_obj)
name = get(event_obj.Target, 'DisplayName');
% Do something with name here
end
Upvotes: 1