问题描述
我使用的是 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
如果它不起作用,请告诉我 :) 祝你好运!