Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Display Cursor Coordinates in matlab

A simple callback function that prints the current cursor location in plot coordinates into the plot window in a user specified location/format/colour.


% cursorLocation - WindowButtonMotionFcn displaying cursor location in plot
% Description : Display the current cursor location within the bounds of a
%               figure window. Assigned as a WindowButtonMotionFcn callback
%               function. Only updates when mouse is moved over plot contents.
% Parameters  : obj         - Figure originating the callback
%               event       - not used (but required)
%               location    - Location within plot window for text. Can be
%                             'BottomLeft', 'BottomRight', 'TopRight', 'TopLeft'
%                             or a [1x2] array of XY location
%               format_str  - A sprintf format string that will accept 2 float
%                             parameters. ie 'X: %.3f, Y: %.3f'
%               text_color  - either a color character (ie 'r') or a RGB
%                             triplet (ie [1.0 1.0 0.5])
% Return      : None
% Usage       : Assign to a Figure WindowButtonMotionFcn callback:
%                 set(fig_handle, 'WindowButtonMotionFcn',
%                     @(obj, event)cursorLocation(obj, event, 'BottomLeft',
%                     'X: %.3f, Y: %.3f', 'r')
% Author      : Rodney Thomson
function cursorLocation(obj, event, location, format_str, text_color)
The cursorLocation function is assigned as the WindowButtonMotionFcn for a figure. Any time the mouse is moved over the specified figure, the callback function will be executed.

The callback function retrieves the cursor location in plot axes coordinates and uses the supplied sprintf format to produce a text label which is printed in a specific location in the plot. This location can be a preset value or an arbitrary [X,Y] coordinate.

Example usage:
t = linspace(-5,5);
y = sinc(t); f = figure; plot(t, y, 'r');
set(f, 'WindowButtonMotionFcn',                           ...
@(obj, event)cursorLocation(obj, event, 'BottomLeft', ...
                          ' X: %.3f\n Y: %.3f', 'r')

If you wanted to avoid setting the WindowButtonMotionFcn callback yourself, you could use the following wrapper function:
function displayCursorLocation(figure_handle, location, format_string, text_color)

  set(figure_handle, 'WindowButtonMotionFcn', ...
      @(obj, event)cursorLocation(obj, event, location, format_string, text_color));


Post a Comment

Follow by Email

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Give Support

Give Support
Encourage me through Comments and by Followers
Related Posts Plugin for WordPress, Blogger...

Special Search For Matlab Projects



Bharadwaj. Powered by Blogger.