0

我正在使用 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.

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

4

1 回答 1

0

您希望在选择数据点时将时间作为第三个值,polarplot为此您可以将时间(变量t)添加ZDatapolarplot.

为此,您需要对极坐标轴使用处理程序,即hax = polarplot(phi, r, '-sb');您可以将时间设置为 ZData: hax.ZData = t;。完成此操作后,变量posinmyfunction的长度将为 3 而不是 2,因此您的if-statement 将被执行。

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

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

h = figure;
hax = polarplot(phi, r, '-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

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

于 2021-05-05T08:23:01.830 回答