|
| 1 | +function [x,y] = select_points(hAx) |
| 2 | +% function [x,y] = select_points(hAx) |
| 3 | +% |
| 4 | +% Interactively draw a rectangle in |
| 5 | +% the current axis and extract its own coordinates. |
| 6 | +% |
| 7 | +% Inputs: |
| 8 | +% |
| 9 | +% hAx [graphics.axis.Axes] - a Matlab Axis class |
| 10 | +% |
| 11 | +% Outputs: |
| 12 | +% |
| 13 | +% x [double] - a row vector of the start/end horizontal |
| 14 | +% rectangle positions |
| 15 | +% y [double] - a row vector of the start/end vertical |
| 16 | +% rectangle positions. |
| 17 | +% |
| 18 | +% Example: |
| 19 | +% |
| 20 | +% %manual evaluation |
| 21 | +% % [x,y] = drawrectangle(gca()); |
| 22 | +% % % draw or click with mose |
| 23 | +% % assert(isnumerical(x)); |
| 24 | +% % assert(isnumerical(y)); |
| 25 | +% |
| 26 | +% |
| 27 | +% author: Rebecca Cowley |
| 28 | + |
| 29 | +% |
| 30 | +if isdeployed || license('test', 'Image_Toolbox') |
| 31 | + rec = drawrectangle(hAx); |
| 32 | + x = [rec.Position(1) rec.Position(1) + rec.Position(3)]; |
| 33 | + y = [rec.Position(2) rec.Position(2) + rec.Position(4)]; |
| 34 | + delete(rec); |
| 35 | +else |
| 36 | + axes(hAx); |
| 37 | + k = waitforbuttonpress; |
| 38 | + point1 = get(gca, 'CurrentPoint'); % button down detected |
| 39 | + finalRect = rbbox; % return figure units |
| 40 | + point2 = get(gca, 'CurrentPoint'); % button up detected |
| 41 | + point1 = point1(1, 1:2); % extract x and y |
| 42 | + point2 = point2(1, 1:2); |
| 43 | + p1 = min(point1, point2); % calculate locations |
| 44 | + offset = abs(point1 - point2); % and dimensions |
| 45 | + x = [p1(1) p1(1) + offset(1) p1(1) + offset(1) p1(1) p1(1)]; |
| 46 | + y = [p1(2) p1(2) p1(2) + offset(2) p1(2) + offset(2) p1(2)]; |
| 47 | + hold on |
| 48 | + axis manual |
| 49 | + plot(x, y); % redraw in dataspace units |
| 50 | +end |
| 51 | + |
| 52 | +end |
0 commit comments