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

collapse all

R2018b: setPositionConstraintFcn is not recommended

Starting in R2018b, a new set of ROI objects replaces the existing set of ROI objects. The new objects provide more functional capabilities, such as face color transparency. The new classes also support events that you can use to respond to changes in your ROI such as moving or being clicked. Although there are no plans to remove the old ROI objects at this time, switch to the new ROIs to take advantage of the additional capabilities and flexibility. For more information on creating ROIs using the new ROI functions, see Create ROI Shapes.

In the existing ROIs, you can create a function that controls where you can draw or move an ROI. You then register the position constraint function with the ROI. To specify the area when you can draw or move an ROI, use the DrawingArea property.

Update Code

Update all instances of setPositionConstraintFcn used with the freehand or polygonal ROI.

Discouraged UsageRecommended Replacement

This example creates a point ROI and uses the setPositionConstraintFcn method to confine ROI creation and movement to within the boundaries of the underlying image.

h = impoint(gca,20,60);
% Make a function that constrains movement of the point
x = get(gca,"XLim");
y = get(gca,"YLim");
fcn = makeConstrainToRectFcn("impoint",x,y);;
% Apply the constraint function to the ROI.

Create one of the new ROI objects and use the DrawingArea property to specify the limits where you can create or move an ROI. For example, this code creates a 10 pixel margin inside the image boundary.

h = drawpoint(gca,"Position",[20 60])
[height width] = size(I); %Get image dimensions
h.DrawingArea = [10,10,(width-20),(height-20)];