微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Matlab:向二维图中的自定义数据提示添加第三个变量

如何解决Matlab:向二维图中的自定义数据提示添加第三个变量

我使用的是 Matlab R2020b,我想在将光标悬停在 2D 图中的数据点上时显示其他信息。我有极坐标图的角度和半径值。每个数据点都与一个时间相关联。我创建了与此类似的图:

t = linspace(0,1,100);
phi = 2*pi*t;
r = t.^2+1;

h = figure;
polarplot(phi,r,'-sb');

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


function output_txt = myfunction(obj,event_obj)
% display data cursor position in a data tip
% obj          Currently not used
% event_obj    Handle to event object
% output_txt   Data tip text,returned as a character vector or a cell array of character vectors

pos = event_obj.Position;


%********* Define the content of the data tip here *********%

% display the x and y values:
output_txt = {['\phi: ' num2str(pos(1)*180/pi) '°'],...
    ['r: ' num2str(pos(2))]};
%***********************************************************%

% If there is a z value,display it:
if length(pos) > 2
    output_txt{end+1} = ['Z',formatValue(pos(3),event_obj)];
end

%***********************************************************%

end

理想情况下,我希望为我选择的任何数据点显示元组(角度、半径、时间)。有关自定义数据提示的信息并没有告诉我如何添加一个变量(时间)的值,只有在使用 3D 绘图(例如 plot3)时才可以。

你知道这个问题的解决方案吗?

解决方法

您希望在选择数据点时将时间作为 polarplot 中的第三个值,为此您可以将时间(变量 t)添加到 ZData 属性polarplot

为此,您需要为 polarplot 轴使用处理程序,即 hax = polarplot(phi,r,'-sb');,以便您可以将时间作为 ZData:hax.ZData = t;。完成此操作后,pos 中的变量 myfunction 的长度将为 3 而不是 2,因此您的 if 语句将被执行。

您更新后的代码应如下所示:

t = linspace(0,1,100);
phi = 2*pi*t;
r = t.^2+1;

h = figure;
hax = polarplot(phi,'-sb');   % New code
hax.ZData = t;                    % New code

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


function output_txt = myfunction(obj,event_obj)
% Display data cursor position in a data tip
% obj          Currently not used
% event_obj    Handle to event object
% output_txt   Data tip text,returned as a character vector or a cell array of character vectors

  pos = event_obj.Position;


  %********* Define the content of the data tip here *********%

  % Display the x and y values:
  output_txt = {['\phi: ' num2str(pos(1)*180/pi) '°'],...
      ['r: ' num2str(pos(2))]};
  %***********************************************************%

  % If there is a z value,display it:
  if length(pos) > 2
      output_txt{end+1} = ['Z',formatValue(pos(3),event_obj)];
  end

  %***********************************************************%

end

如果它不起作用,请告诉我 :) 祝你好运!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?