Tuesday, July 29, 2008

Display Cursor Coordinates

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

DisplayCursorLocation

% 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
% http://iheartmatlab.blogspot.com
%===============================================================================
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));
end

5 comments:

  1. Blogs are so informative where we get lots of information on any topic. Nice job keep it up!!
    _____________________________

    Doctoral Dissertation

    ReplyDelete
  2. Hello,
    I am working on a project were I want to connect to multiple robots (Brookstone Rover) at once. The issue is that all robots have the same IP address (it cannot be changed). They only differ by the ssid of the adhoc network. I've written some pure MATLAB code were I can connect to a robot by creating a tcip object and then control it. But now I want connection to more than one robot at a time.

    So to solve part of the problem, I've purchased several USB wifi dongles, and I connect each robot to an individual dongle by means of the ssid. Then, I managed to connect to a specific USB dongle (NIC) by using the JAVA SocketChannel library imported into MATLAB, and then binding to my dongle by telling my socket the name of my USB dongle, and then use the 'bind' JAVA function. But for some reason I cannot send commands to the robot after I've successfully connected. I get an error that says that my 'write' function is not defined for the object I've created.
    So then I looked at your MATLAB code, and I was wondering if you have any idea how I could use your code to attain my goal of connected to a specific wifi dongle, and then be able to send the commands to the robot through each dongle.

    If you could tell me how to use your code for this, it would be of great help to me!

    Thank you very much!!!!

    ReplyDelete
  3. Send me an email (and perhaps some sample code - it looks like a datatype / pathing problem).

    Blog post comments aren't the best place for specific advice discussion.

    Rodney

    ReplyDelete
  4. Looks very useful. I see that it does not work properly on a Weibull plot (this one's tricky because it uses a loglog vs log scale). I have no time to check it but just by CPU use it seems to be computed but the result is simply not visible inside the plot.

    ReplyDelete
  5. re prev. post. Seems like with my Weibull plots the "None or too many axes to draw into" condition causes a return. This is probably because my figure was generated by calling plot more than once. Greetings

    ReplyDelete