Main Content

dragrect

Drag rectangles with mouse

Syntax

[finalrect] = dragrect(initialrect)
[finalrect] = dragrect(initialrect,stepsize)

Description

[finalrect] = dragrect(initialrect) tracks one or more rectangles anywhere on the screen. The n-by-4 matrix initialrect defines the rectangles. Each row of initialrect must contain the initial rectangle position as [left bottom width height] values. dragrect returns the final position of the rectangles in finalrect.

[finalrect] = dragrect(initialrect,stepsize) moves the rectangles in increments of stepsize. The lower left corner of the first rectangle is constrained to a grid of size equal to stepsize starting at the lower left corner of the figure, and all other rectangles maintain their original offset from the first rectangle.

[finalrect] = dragrect(...) returns the final positions of the rectangles when the mouse button is released. The default step size is 1.

Examples

collapse all

To track a rectangle in a figure, first create a program file called trackRectangle.m. Within the program file:

  • Create a figure and return the Figure object.

  • Block statements from executing until you click a mouse button by using the waitforbuttonpress function.

  • Make a 50-by-100 pixel rectangle appear by using the dragrect function. The lower-left corner of the rectangle is at the cursor position.

  • Drag the rectangle to a different position. When you release the mouse button, the dragrect function returns the final position of the rectangle.

function r2 = trackRectangle
    f = figure;
    waitforbuttonpress
    p = f.CurrentPoint;
    r1 = [p(1,1) p(1,2) 50 100];
    r2 = dragrect(r1);
end

Run the program file. Track a rectangle by clicking and dragging.

trackRectangle
ans =

   330   275    50   100

To track a rectangle within an Axes object, you must first disable built-in interactions. Otherwise, when you drag the rectangle, the axes will pan (in a 2-D view) or rotate (in a 3-D view). For more information about built-in interactions, see Control Chart Interactivity.

Create a program file called trackRectangleInAxes.m. Within the program file:

  • Return the current figure as a variable.

  • Block statements from executing until you click a mouse button by using the waitforbuttonpress function.

  • Make a 50-by-100 pixel rectangle appear by using the dragrect function. The lower-left corner of the rectangle is at the cursor position.

  • Drag the rectangle to a different position. When you release the mouse button, the dragrect function returns the final position of the rectangle.

function r2 = trackRectangleInAxes
    f = gcf;
    waitforbuttonpress
    p = f.CurrentPoint;
    r1 = [p(1,1) p(1,2) 50 100];
    r2 = dragrect(r1);
end

Then, create a chart. Disable built-in interactions by calling the disableDefaultInteractivity function.

plot(1:10)
ax = gca;
disableDefaultInteractivity(ax)

Call the program file. Track the rectangle by clicking and dragging.

trackRectangleInAxes
ans =

   330   275    50   100

After tracking the rectangle, you can reenable built-in interactions by calling the enableDefaultInteractivity function.

enableDefaultInteractivity(ax)

More About

collapse all

Pixels

Distances in pixels are independent of your system resolution on Windows® and Macintosh systems:

  • On Windows systems, a pixel is 1/96th of an inch.

  • On Macintosh systems, a pixel is 1/72nd of an inch.

On Linux® systems, the size of a pixel is determined by your system resolution.

Tips

dragrect returns immediately if a mouse button is not currently pressed. Use dragrect in a ButtonDownFcn, or from the command line in conjunction with waitforbuttonpress, to ensure that the mouse button is down when dragrect is called. dragrect returns when you release the mouse button.

If the drag ends over a figure window, the positions of the rectangles are returned in that figure's coordinate system. If the drag ends over a part of the screen not contained within a figure window, the rectangles are returned in the coordinate system of the figure over which the drag began.

Note

You cannot use normalized figure units with dragrect.

Version History

Introduced before R2006a

expand all