stm
stm

Reputation: 160

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

I want to create tooltips for each line in a treeplot-similar plot in MATLAB. I used code examples to create the following script that shows whether the mouse is over a line object:

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

In my further project I need multiple lines in the plot. The simple example in the following shows that two lines are plotted. However, the result in the command window is always "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

Is there another approach to check whether the mouse is over a line object or not?

Upvotes: 0

Views: 509

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

In mousemove, test for any(obj == L). This tests whether any of the elements in L match obj. Since obj==L will at most have one non-zero element, this always evaluates to 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.

Upvotes: 1

Related Questions