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
0 comments:
Post a Comment