【问题标题】:Data tip customization in matlab figurematlab图中数据提示自定义
【发布时间】:2022-12-21 01:07:35
【问题描述】:

我有一个包含多个图的图表,每个图都来自不同的源文件。我希望数据提示告诉我 (X,Y) 加上源文件的名称。这么久我最好的尝试(没有成功)是这样的:

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

在哪里我的功能是在这种情况下使用的默认函数,如粘贴在此消息末尾和此处所述: 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

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      我来晚了一点,但我想我会回答,以防万一有人遇到这个问题并且仍然觉得它有用。

      改变

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

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

      然后可以将回调函数更改为如下所示。 (注意:我删除了 Z 坐标,因为问题只提到了 X 和 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
      

      显然,如果您想要不同格式的字符串,您可以使用回调函数中的格式做任何您想做的事情。

      您可以向回调函数添加任意数量的参数,只需更改其函数签名并更新 set(dcm,... 行以匹配(其他参数位于 {} 内,以逗号分隔)。这适用于 R2013a(我稍后假设),但我没有在任何早期版本上尝试过。

      编辑:回调函数可能还需要在与使用它的代码相同的文件中定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2015-02-11
        • 1970-01-01
        • 2010-11-15
        相关资源
        最近更新 更多