Main Content


Set position constraint function of ROI object

setPositionConstraintFcn is not recommended. With the new ROIs, use the DrawingArea property instead. For more information, see Compatibility Considerations.



setPositionConstraintFcn(h,fcn) sets the position constraint function of the ROI object h to be the specified function handle, fcn. Whenever the object is moved because of a mouse drag, the constraint function is called using the syntax:

constrained_position = fcn(pos)


collapse all

Display a rectangle ROI over an image.

h = imrect(gca,[10 10 100 100]);

Display the position of the rectangle in the title.

addNewPositionCallback(h,@(p) title(mat2str(p,3)));

The title updates when you move the rectangle. Try dragging one side of the rectangle outside the boundary of the image.

Specify a position constraint function using makeConstrainToRectFcn to keep the rectangle inside the original XLim and YLim ranges.

fcn = makeConstrainToRectFcn("imrect",get(gca,"XLim"),get(gca,"YLim"));

Now drag the rectangle using the mouse. Observe that the rectangle can no longer extend past the image boundary.

Input Arguments

collapse all

ROI object, specified as an imellipse, imline, impoint, impoly, or imrect object.

Function handle, specified as a handle. You can use the makeConstrainToRectFcn to create this function. The function must accept a numeric array as input, and it must return a numeric array as output. Both arrays must have the same form as returned when calling getPosition on the object. For more information, see Create Function Handle.

Version History

Introduced in R2008a

expand all