How do I let a user select an area to zoom in an axis in my GUI?

2 views (last 30 days)
How do I let a user select an area to zoom in an axis in my GUI?
I have a plot in a GUI window. I want the user to be able to push a button, select a rectangular area, and bring up a zoomed-in view of that area in another figure window. I wanted to use the RBBOX command, but it doesn't return the information about the boundary of the box in the right form, plus I will need to get the data to plot in the other figure. How can I do this? Is there a command to do it?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
There is no one command which does this. Most of the complexity of this problem relies on converting the output from RBBOX to actual X and Y coordinates. Assuming that your data is in vectors XDATA and YDATA, the code below does this:
% ax is the handle to the axes where the plot is, fg is the handle to the
% figure
if ~exist('ax')
ax = gca;
end
if ~exist('fg')
fg = gcf;
end
k=waitforbuttonpress;
BOUND = rbbox; % Get the boundary of the box
funits = get(fg,'Units');
aunits = get(ax,'Units'); % Store current units for axes and figure in order to restore them at the end of the routine.
set(fg,'Units','pixels'); % Set the figure units to 'pixels'
set(ax,'Units','pixels'); % Set the axis units to 'pixels'
P = get(ax, 'Position'); % Retrieve the axes position.
XLimit = get(ax, 'Xlim'); YLimit = get(ax, 'Ylim');
% Get the coordinates of the lower left corner of the box
% Its height and width, and the X-Y coordinates of the 4 corners
DeltaX = XLimit(2)-XLimit(1); DeltaY = YLimit(2)-YLimit(1);
LeftDist = BOUND(1)-P(1); UpDist = BOUND(2)-P(2);
% Defining some useful quantities which will be used often
XLow = XLimit(1)+DeltaX*LeftDist/P(3);
XHigh = XLimit(1)+DeltaX*(LeftDist+BOUND(3))/P(3);
YLow = YLimit(1)+DeltaY*UpDist/P(4);
YHigh = YLimit(1)+DeltaY*(UpDist+BOUND(4))/P(4);
% This is the X-Y information about the corners of the RBBOX
XBounds = [XLow, XHigh]; YBounds = [YLow, YHigh];
% ZOOM IN: Set the axes limits according to the zoom box
set(ax,'XLim',XBounds,'YLim',YBounds);
set(fg,'Units',funits);
set(ax,'Units',aunits); % Reset the figure and axis units to their original settings

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2007a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!