gpt4 book ai didi

matlab - matlab图中数据提示自定义

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:52 24 4
gpt4 key购买 nike

我有一个包含多个图的图表,每个图都来自不同的源文件。我希望数据提示告诉我 (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

最佳答案

p=plot( x,y);
setappdata(p,'sourceFile_whatever', SourceFileName)

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

在回调函数中:

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

关于matlab - matlab图中数据提示自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12100086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com