我有一个图形,其中包含几个绘图,每个绘图都来自不同的源文件。我希望数据提示能告诉我(X,Y)以及源文件的名称。到目前为止,我最好的尝试(没有成功)是这样的:
dcm = datacursormode(gcf);
datacursormode on;
set(dcm,'UpdateFcn',[@myfunction,{SourceFileName}]);
在这种情况下,默认函数是 myfunction,该函数在本消息末尾粘贴,并在此处解释:http://blogs.mathworks.com/videos/2011/10/19/tutorial-how-to-make-a-custom-data-tip-in-matlab/。最后,SourceFileName 是一个字符串,它包含源文件的名称。
是否有更简单(或正确)的方法来完成这个任务?
提前致谢。
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