-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursorLocation.m
115 lines (100 loc) · 4.83 KB
/
cursorLocation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% 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)
% find axes associated with calling window
axes_handle = findobj(obj, 'Type', 'axes');
% Only support figures with 1 axes (Avoids confusion)
if (isempty(axes_handle) || (numel(axes_handle) > 1))
% None or too many axes to draw into
return;
end
% Format the text string
curr_point = get(axes_handle, 'CurrentPoint');
position = [curr_point(1, 1) curr_point(1, 2)];
position_string = sprintf(format_str, position);
user_data = get(axes_handle, 'UserData');
% If this is the first callback call, then will need to initialise the text
% object and save to the axes in UserData
if (~isfield(user_data, 'text_handle') || ~ishandle(user_data.text_handle))
user_data.text_handle = text(nan, nan, '', ...
'Parent', axes_handle, ...
'Color', text_color);
set(axes_handle, 'UserData', user_data); % save to axes
end
% work out where we need to put the text
[x_lims y_lims in_bounds] = getBounds(axes_handle, position);
if (~in_bounds)
% Not in the plot bounds so dont update
return;
end
% Default to bottom left
vertical_alignment = 'bottom';
horizontal_alignment = 'left';
text_position = [x_lims(1) y_lims(1)];
if (ischar(location))
switch location
case 'BottomLeft'
vertical_alignment = 'bottom';
horizontal_alignment = 'left';
text_position = [x_lims(1) y_lims(1)];
case 'BottomRight'
vertical_alignment = 'bottom';
horizontal_alignment = 'right';
text_position = [x_lims(2) y_lims(1)];
case 'TopRight'
vertical_alignment = 'top';
horizontal_alignment = 'right';
text_position = [x_lims(2) y_lims(2)];
case 'TopLeft'
vertical_alignment = 'top';
horizontal_alignment = 'left';
text_position = [x_lims(1) y_lims(2)];
otherwise
% Do nothing -> Default
end
elseif (isnumeric(location) && (numel(location) == 2))
% Numeric... user sets text bottom left position
text_position(1) = location(1);
text_position(2) = location(2);
end
set(user_data.text_handle, 'Position', text_position, ...
'String', position_string, ...
'VerticalAlignment', vertical_alignment, ...
'HorizontalAlignment', horizontal_alignment);
end
%===============================================================================
% Description : Return the bounds of the supplied axes and identify whether
% supplied position is contained within axes bounds
%===============================================================================
function [x_lims y_lims in_bounds] = getBounds(axes_handle, position)
x_lims = get(axes_handle, 'XLim');
y_lims = get(axes_handle, 'YLim');
in_bounds = (position(1) >= x_lims(1)) && ...
(position(1) <= x_lims(2)) && ...
(position(2) >= y_lims(1)) && ...
(position(2) <= y_lims(2));
end