MATLAB Answers

0

Odd getrect(FIG) behavior with subplots (r2015b)

Asked by Peter Cook on 18 Oct 2017
Latest activity Answered by Peter Cook on 24 Jan 2018
I am noticing an unusual behavior of getrect when passing a figure handle as the argument. e.g.
hFig1 = figure(1);
hAx1 = subplot(2,1,1);
plot(randn(100),randn(100),'.');
%hFig1.CurrentAxes is hAx1
hAx2 = subplot(2,1,2);
plot(randn(100),randn(100),'.');
%hFig1.CurrentAxes is now hAx2
%
%call getrect and select a rectangle on hAx1 (subplot 121)
>> r = getrect(hFig1)
r =
-2.9528 10.5594 6.8848 6.7133
%
%call getrect again and select another rectangle on hAx1 (subplot 121)
>> r = getrect(hFig1)
r =
-3.0138 -3.1389 6.0829 6.2778
Note the difference in r(2)//ymin - it seems that CurrentAxes property of hFig1 gets reassigned after the call to getrect.
Is is possible to either:
1. Interrupt getrect, reassign the CurrentAxes property of hFig1 to the subplot which has been clicked, then resume getrect?
or
2. Prevent the reassignment of the CurrentAxes property of hFig1 by getrect?

  2 Comments

Jan
on 18 Oct 2017
Note the difference in r(2)/ymin
I note a difference, but why is this unexpected? You have selected two different rectangles. Why do you assume a relation to the CurrentAxes? Please explain the problem again.
r is returned not in figure coordinates, but data coordinates. The expected behavior for me is to return a rectangle corresponding to data coordinates of the axis on which I draw the rectangle.
In the example above, I have drawn 2 similar rectangles on the same subplot - the coordinates returned from rectangle 1 are in data coordinates of hAx2, the coordinates returned from the second rectangle I make on the same axes are in data coordinates of hAx1.

Sign in to comment.

2 Answers

Answer by Peter Cook on 24 Jan 2018
 Accepted Answer

Since the desired subplot is not known ahead of time, I ended up using this block of code to first figure out which axes was clicked on, and then collect the rectangle coordinates using rbbox.
%wait for user input.
%determine if mouse or keyboard has been pressed
%only continue if left mouse button is clicked
keyDown = waitforbuttonpress;
isLeftMouseClick = strcmp(handles.output.SelectionType,'normal');
if keyDown == 0 && isLeftMouseClick
%keyDown 1 = keyboard
%keyDown 0 = mouse click
pt1 = handles.output.CurrentPoint;
%determine which axes has been clicked on
whichAxes = 0;
for kAxes = 1:6
if inpolygon(pt1(1),pt1(2),xv(kAxes,:),yv(kAxes,:))
whichAxes = kAxes;
break
end
end
if ~whichAxes
%dont do anything
else
%get extents with mouse
finalRect = rbbox;
...

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 19 Oct 2017

From the help for the getrect function:
"RECT = getrect(FIG) lets you select a rectangle in the current axes of figure FIG using the mouse.
RECT = getrect(AX) lets you select a rectangle in the axes specified by the handle AX."
If I understand correctly what you want, I think the best solution is to use hAx1 and hAx2 in your getrect calls instead of trying to fiddle with which axes is current in the figure hFig1.

  0 Comments

Sign in to comment.