Wednesday, January 25, 2012

MATLAB GUI: Finding the 'hotspot' (the missing mouseover function)

In the third tutorial of the series, we will look at a walk-around for the missing mouse over function for the uicontrols. We will be coding in the WindowButtonMotionFcn callback. Whenever the mouse moves in the figure, we will capture the mouse location, capture the boundaries of the uicontrol, and finally, determine whether the mouse is over the uicontrol or not. 


Step-by-step guide
1. Download mouse2.zip and ready the files in a working folder in MATLAB. 
2. Add a static text to the GUI, rename its tag to lbl_target and set its unit to pixel 
3. Copy and paste the following code to the end of the WindowButtonMotionFcn 

% get position information of the uicontrol 
bounds = get(handles.lbl_target,'position');
lx = bounds(1); ly = bounds(2);
lw = bounds(3); lh = bounds(4);
% test to see if mouse is within the uicontrol. 
if x >= lx && x <= (lx + lw) && y >= ly && y <= (ly + lh)
    set(handles.lbl_target, 'string', 'IN');
    set(handles.lbl_target, 'backgroundcolor', 'red');
else
    set(handles.lbl_target,'string', 'OUT');
    set(handles.lbl_target, 'backgroundcolor', 'green');    
end


4. Save all files and run the GUI. 


Video 




Files
Files can be downloaded here

1 comment: