-
Notifications
You must be signed in to change notification settings - Fork 42
/
pickregion.m
104 lines (84 loc) · 3.06 KB
/
pickregion.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
%PICKREGION Pick a rectangular region of a figure using mouse
%
% [p1,p2] = PICKREGION() initiates a rubberband box at the current click point
% and animates it so long as the mouse button remains down. Returns the first
% and last coordinates in axis units.
%
% Options::
% 'axis',A The axis to select from (default current axis)
% 'ls',LS Line style for foreground line (default ':y');
% 'bg'LS, Line style for background line (default '-k');
% 'width',W Line width (default 2)
% 'pressed' Don't wait for first button press, use current position
%
% Notes::
% - Effectively a replacement for the builtin rbbox function which draws the box in
% the wrong location on my Mac's external monitor.
%
% Author::
% Based on rubberband box from MATLAB Central written/Edited by Bob Hamans
% (B.C.Hamans@student.tue.nl) 02-04-2003, in turn based on an idea of
% Sandra Martinka's Rubberline.
function [p1,p2]=pickregion(varargin)
% handle options
opt.axis = gca;
opt.ls = ':y';
opt.bg = '-k';
opt.width = 2;
opt.pressed = false;
opt = tb_optparse(opt, varargin);
h = opt.axis;
% Get current user data
cudata=get(gcf,'UserData');
hold on;
% Wait for left mouse button to be pressed
if ~opt.pressed
k=waitforbuttonpress;
end
% get current point
p1=get(h,'CurrentPoint'); %get starting point
p1=p1(1,1:2); %extract x and y
% create 2 overlaid lines for contrast:
% black solid
% color dotted
lh1 = plot(p1(1),p1(2),opt.bg, 'LineWidth', opt.width); %plot starting point
lh2 = plot(p1(1), p1(2), opt.ls, 'LineWidth', opt.width);
% Save current point and handles in user data
udata.p1=p1;
udata.h=h;
udata.lh1=lh1;
udata.lh2=lh2;
% Set handlers for mouse up and mouse motion
udata.wbupOld = get(gcf, 'WindowButtonUp');
udata.wbmfOld = get(gcf, 'WindowButtonMotionFcn');
set(gcf, ...
'WindowButtonMotionFcn', @(src,event) wbmf(src,udata), ...
'WindowButtonUp', @(src,event) wbup(src,udata), ...
'DoubleBuffer','on');
% Wait until the lines have been destroyed
waitfor(lh1);
% Get data for the end point
p2=get(h,'Currentpoint'); %get end point
p2=p2(1,1:2); %extract x and y
% Remove the mouse event handlers and restore user data
set(gcf,'UserData',cudata,'DoubleBuffer','off');
end
function wbmf(src, ud) %window motion callback function
% get current coordinates
P = get(ud.h,'CurrentPoint');
P = P(1,1:2);
% Use 5 point to draw a rectangular rubberband box
xdata = [P(1),P(1),ud.p1(1),ud.p1(1),P(1)];
ydata = [P(2),ud.p1(2),ud.p1(2),P(2),P(2)];
% draw the two lines
set(ud.lh1,'XData', xdata,'YData', ydata);
set(ud.lh2,'XData', xdata,'YData', ydata);
end
function wbup(src, ud)
% remove motion handler
set(gcf, 'WindowButtonMotionFcn', ud.wbmfOld);
set(gcf, 'WindowButtonUpFcn', ud.wbupOld);
% delete the lines
delete(ud.lh2);
delete(ud.lh1);
end