主要内容

dragrect

用鼠标拖动矩形

Syntax

[finalRect] = dragrect(initial retrect)
[finalRect] = dragrect(初始rect,stepsize)

Description

[finalRect] = dragrect(initial retrect)tracks one or more rectangles anywhere on the screen. The n-by-4 matrixinitialrect定义矩形。每一行initialrectmust contain the initial rectangle position as [left bottom width height]values.dragrectreturns the final position of the rectangles in最终优势

[finalRect] = dragrect(初始rect,stepsize)以增量的增量移动矩形步长。th的左下角e first rectangle is constrained to a grid of size equal to步长从图的左下角开始,所有其他矩形都从第一个矩形保持原始偏移。

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

Examples

collapse all

要跟踪图中的矩形,首先创建一个名为的程序文件trackRectangle.m。Within the program file:

  • 创建一个图形并返回Figure目的。

  • Block statements from executing until you click a mouse button by using thewaitforbuttonpress功能。

  • Make a 50-by-100 pixel rectangle appear by using thedragrect功能。The lower-left corner of the rectangle is at the cursor position.

  • 将矩形拖到不同的位置。发布鼠标按钮时,dragrect功能returns the final position of the rectangle.

功能r2 = trackRectangle f =图;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 anAxesobject, 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, seeControl Chart Interactivity

Create a program file calledTrackRectangleinaxes.m。Within the program file:

  • Return the current figure as a variable.

  • Block statements from executing until you click a mouse button by using thewaitforbuttonpress功能。

  • Make a 50-by-100 pixel rectangle appear by using thedragrect功能。The lower-left corner of the rectangle is at the cursor position.

  • 将矩形拖到不同的位置。发布鼠标按钮时,dragrect功能returns the final position of the rectangle.

功能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 thedisableDefaultInteractivity功能。

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

调用程序文件。通过单击和拖动跟踪矩形。

trackRectangleInAxes
ans = 330 275 50 100

After tracking the rectangle, you can reenable built-in interactions by calling theenableDefaultInteractivity功能。

启用efaultEnteractivity(AX)

More About

collapse all

Pixels

Distances in pixels are independent of your system resolution on Windows®Macintoshsystems:

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

  • Macintosh系统,像素是1/72的1英寸。

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

提示

dragrectreturns immediately if a mouse button is not currently pressed. Usedragrectin aButtonDownFcn, or from the command line in conjunction withwaitforbuttonpress, to ensure that the mouse button is down whendragrectis called.dragrectreturns 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 withdragrect

Version History

在R2006a之前引入

expand all

R2020b中的行为发生了变化