问题
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
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/12100086/data-tip-customization-in-matlab-figure