MATLAB:使用 WindowButtonMotionFcn 为绘图中的多条线对象创建工具提示

MATLAB: Creating tooltips for multiple line objects in a plot using WindowButtonMotionFcn

我想在 MATLAB 中为类似树状图的每一行创建工具提示。我使用代码示例创建了以下脚本,显示鼠标是否位于线对象上:

function test_mouseover2
f=figure(1);
axis([0 1 0 1]);
L=line([0.2500 0.5000], [0.6 0.8], 'Color','red');    
set(f,'WindowButtonMotionFcn',{@mousemove,L,process});
end

function mousemove(src,ev,L,process)
obj = hittest(src);
if obj == L
    disp('Yes');
else
    disp('No');
end
end

在我的进一步项目中,我需要在情节中多行。下面的简单示例显示绘制了两条线。但是,命令 window 中的结果总是 "No":

function test_mouseover2
f=figure(1);
axis([0 1 0 1]);
L=line([0.2500 0.5000; 0.125 0.25], [0.6 0.8; 0.2 0.6], 'Color','red');

set(f,'WindowButtonMotionFcn',{@mousemove,L,process});
end

function mousemove(src,ev,L,process)
obj = hittest(src);
if obj == L
    disp('Yes');
else
    disp('No');
end
end

是否有另一种方法来检查鼠标是否在直线对象上?

mousemove 中,测试 any(obj == L)。这将测试 L 中的任何元素是否匹配 obj。因为 obj==L 最多有一个非零元素,所以这总是计算为 false as far as if is concerned:

An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.