Code covered by the BSD License  

Highlights from
Data Cursor on Mouse Move

image thumbnail

Data Cursor on Mouse Move

by

 

15 Sep 2011 (Updated )

Display and update the pixel value from image in the figure title bar on mouse movement.

getPixelOnMouseMove(obj,event_obj,varargin)
function getPixelOnMouseMove(obj,event_obj,varargin)
%GETPIXELONMOUSEMOVE  Display and update the pixel value from image in the figure
% title bar on mouse movement.
%
% USAGE:
%        GETPIXELONMOUSEMOVE(obj,event_obj)
%        GETPIXELONMOUSEMOVE(obj,event_obj,imagePlot)
%
% INPUT:
%    obj - Figure handle originating the callback
%    event_obj - not used (but required)
%    imagePlot (optional) - handel to image plot
%
% OUTPUT:
%
% EXAMPLES:
%    Assign to a Figure WindowButtonMotionFcn callback:
%    z=rand(10,10);
%    image(z,'CDataMapping','scaled')
%    set(gcf,'WindowButtonMotionFcn', @getPixelOnMouseMove);
%
% LIMITATIONS:
%    Tested only with "image" function
%
% See also:
%
% HISTORY
% 1.0.0 - Initial release 15-Sep-2011
% 1.0.1 16-Sep-2011
%       - bug fixed on subfunction "closest"
%       - Added comments
% 1.1.0 11-June-2012
%       - update with overobj3 subfunction which returns handle of object
%         that the pointer is over
%       - third argument (imagePlot) is optional, in this case overobj3 
%         subfunction returns the handle of image that the pointer is over. 
%         This is useful if there are subplots with images.

% Author:Durga Lal Shrestha
% CSIRO Land & Water, Highett, Australia
% eMail: durgalal.shrestha@gmail.com
% Website: www.durgalal.co.cc
% Copyright 2011 Durga Lal Shrestha
% $First created: 15-Sep-2011
% $Revision: 1.1.0 $ $Date: 11-June-2012 09:35:00 $

% ***********************************************************************
%% INPUT ARGUMENTS CHECK
error(nargchk(2,3,nargin));
% Check if there is image
if nargin>2
    imagePlot = varargin{1};
    if ~strcmp(get(imagePlot,'type'),'image')
        error('getPixelOnMouseMove','Third Argument to getPixelOnMouseMove requires handle to image')        
    end            
else
    imagePlot = overobj3('type','image');
end
display= false;
if ~isempty(imagePlot)
    axes_handle = overobj3('type','axes');
    %axes_handle = get(obj, 'CurrentAxes');   % Get handle of current axis
    pos = get(axes_handle, 'CurrentPoint');  % Get position of current mouse location
    if ~isempty(pos)
        x =  pos(1,1);   % x position of mouse location
        y = pos(1,2);    % y position of mouse location
        
        x_lims = get(axes_handle, 'XLim');
        y_lims = get(axes_handle, 'YLim');
        
        marginx = (x_lims(2)-x_lims(1))*0.0005;  % allow small margin
        marginy = (y_lims(2)-y_lims(1))*0.0005;
        
        % If the mouse location is inside the axis bounds,
        % then display the pixel
        if (x>=x_lims(1)- marginx && x<=x_lims(2)+ marginx) && ...
                (y>=y_lims(1)- marginy && y<=y_lims(2)+ marginy)
            
            % Now get xdata and ydata of the image plot
            xx=get(imagePlot,'xdata');
            yy=get(imagePlot,'ydata');
            
            % Get the cdata of the image
            z=get(imagePlot,'cdata');
            
            % Whether image(z) is used instead of image(x,y,z) by checking size of
            % xx and yy with z
            [row col]=size(z);
            if row ~= length(yy) || col ~= length(xx)
                xx=1:col;
                yy=1:row;
            end
            
            % Get the corresponding index of the closest points in xx for x or in
            % yy for y
            xind=closest(xx,x);
            yind=closest(yy,y);
            
            % Get the pixel value at mouse location by snaping to nearest data vertex
            pvalue = z(yind,xind);
            
            % Prepare for text to be writen on figure caption bar
            txt = ['X: ' num2str(pos(1,1)) '   Y: ' num2str(pos(1,2)) '   Z: ' num2str(pvalue)];
            
            % Write the coordinates and pixel value in Figure title bar
            set(obj,'NumberTitle','off','Name',txt);
            display=true;
            %else %If the mouse location is outside the axis bounds,
            % then do not display the pixel value
            %   set(obj,'NumberTitle','on','Name','')
        end        
    end
end
if ~display
    set(obj,'NumberTitle','on','Name','')
end
%--------------------------------------------------------------------------
function k = closest(X,xi)
% Returns the indices k of the closest points in X for each point in xi
%
n=length(xi);
k=zeros(n,1);

for i=1:n
    dist = abs(X-xi(i));
    [tmp  ind] =sort(dist);
    k(i)=ind(1);
end

%%-------------------------------------------------------------------------
function h = overobj3(varargin)
%OVEROBJ3 Get handle of object that the pointer is over.
%   H = OVEROBJ3 searches all objects in the PointerWindow
%   looking for one that is under the pointer. Returns first
%   object handle it finds under the pointer, or empty matrix.
%
%   H = OVEROBJ3(FINDOBJ_PROPS) searches all objects which are
%   descendants of the figure beneath the pointer and that are
%   returned by FINDOBJ with the specified arguments.
%
% OVEROBJ3 is modification of overobj and overobj2
% (http://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions/
% ). overobj and overobj2 do not return handle to image. OVEROBJ3 
% returns handle to image too.
%
%   Example:
%       h = overobj3('type','image');
%       h = overobj3('flat','visible','on');
%
% See also OVEROBJ, FINDOBJ

% Author:Durga Lal Shrestha
% CSIRO Land & Water, Highett, Australia
% eMail: durgalal.shrestha@gmail.com
% Website: www.durgalal.co.cc
% Copyright 2011 Durga Lal Shrestha
% $First created: 11-June-2012
% $Revision: 1.0.0 $ $Date: 11-June-2012 09:35:00 $
 
%% Ensure root units are pixels
oldUnits = get(0,'units');
set(0,'units','pixels');
 
% Get the figure beneath the mouse pointer & mouse pointer pos
fig = get(0,'PointerWindow'); 
p = get(0,'PointerLocation');
set(0,'units',oldUnits);
 
% Look for quick exit (if mouse pointer is not over any figure)
if fig==0,  h=[]; return;  end
 
% Compute figure offset of mouse pointer in pixels
figPos = getpixelposition(fig);
x = (p(1)-figPos(1));
y = (p(2)-figPos(2));


c = findobj(get(fig,'Children'),varargin{:});
if ~strcmp(get(c,'type'),'image') % There is no 'Position' property in the 'image' class
    % Loop over all figure descendents
    for h = c',
        % If descendent contains the mouse pointer position, exit
        r = getpixelposition(h);      
        if (x>r(1)) && (x<r(1)+r(3)) && (y>r(2)) && (y<r(2)+r(4))
            return
        end
    end
    h = [];
else % if child of figure is image
    % Since there is no 'Position' property in the 'image' class, find the
    % handle of axes, and get the child of the axes
    c = findobj(get(fig,'Children'),'type','axes');
    %% Loop over all axes descendents
    for h = c',
        % If descendent contains the mouse pointer position, exit
        r = getpixelposition(h);
        if (x>r(1)) && (x<r(1)+r(3)) && (y>r(2)) && (y<r(2)+r(4))
            image_handle = findobj(get(h,'Children'),'type','image');
            if ~isempty(image_handle)
                h = image_handle;
                return
            end
        end
    end
    h=[];
end

Contact us