Data tip customization in matlab figure
Asked Answered
H

2

4

I have a graph with several plots, each of them comes from a different source file. I want the data tip to tell me (X,Y) plus the name of the source file. So long my best try (without success) is this:

dcm = datacursormode(gcf);
datacursormode on;
set(dcm,'UpdateFcn',[@myfunction,{SourceFileName}]);

Where myfunction is the default function used in this cases, as pasted at the end of this message and as explained here: http://blogs.mathworks.com/videos/2011/10/19/tutorial-how-to-make-a-custom-data-tip-in-matlab/ Finally, SourceFileName is a string with the name of the source file.

Does anybody knows an easier (or correct) way to do this?

Thanks in advance.

function output_txt = myfunction(~,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).

pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)]};

% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end

end
Heretic answered 23/8, 2012 at 21:16 Comment(0)
S
3
p=plot( x,y);
setappdata(p,'sourceFile_whatever', SourceFileName)  

dcm = datacursormode(gcf);
datacursormode on;
set(dcm, 'updatefcn', @myfunction)

and in callback function:

function output_txt = myfunction( 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).
% event_obj

dataIndex = get(event_obj,'DataIndex');
pos = get(event_obj,'Position');

output_txt = {[ 'X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)]};

try
    p=get(event_obj,'Target');
    output_txt{end+1} = ['SourceFileName: ',getappdata(p,'sourceFile_whatever')];
end


% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
Spouse answered 28/1, 2013 at 16:14 Comment(0)
C
0

I'm a little late to the game, but I thought I would answer in case anyone comes across this question and still finds it useful.

Change

set(dcm,'UpdateFcn',[@myfunction,{SourceFileName}]);

to

set(dcm,'UpdateFcn',{@myfunction,SourceFileName});

Then the callback function can be changed to something like the following. (Note: I removed the Z coordinate because the question mentioned only X and Y.)

function output_txt = myfunction(~,event_obj,filename)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% filename     Name of the source file (string)
% output_txt   Data cursor text string (string or cell array of strings).

pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)],...
    ['Source: ',filename]};

end

Obviously you can do anything you want with the formatting inside the callback function in case you want the string in a different format.

You can add any number of arguments to the callback function simply by changing its function signature and updating the set(dcm,... line to match (additional arguments go inside the {}, separated by commas). This works for R2013a (and I assume later), but I have not tried it on any earlier versions.

EDIT: The callback function may also need to be defined in the same file as the code that uses it.

Columbus answered 12/3, 2014 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.