Main Content


Add new-position callback to ROI object

addNewPositionCallback is not recommended. With the new ROIs, use the addlistener object function instead. For more information, see Compatibility Considerations.



id = addNewPositionCallback(h,fcn) adds the function handle fcn to the list of new-position callback functions of the ROI object h. Whenever the ROI object changes its position, each function in the list is called with the syntax:


pos is of the form returned by the object's getPosition method. The return value, id, is used only with removeNewPositionCallback.


collapse all

Create a rectangle ROI object. Display the position of the rectangle in the title. The title updates when you move the rectangle.

h = imrect(gca, [10 10 100 100]);
addNewPositionCallback(h,@(p) title(mat2str(p,3)));

Now drag the rectangle using the mouse to observe the callback behavior.

Input Arguments

collapse all

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

Function handle, specified as a handle. The function must accept a numeric array as input. The array must have the same form as returned when calling getPosition on the object. For more information, see Create Function Handle.

Output Arguments

collapse all

Identifier of new-position callback function, returned as a struct.

Version History

Introduced in R2008a

collapse all

R2018b: addNewPositionCallback 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.

With the new ROIs, the Position property contains the current location of the ROI. To receive notification when this value changes, set up a listener using the addlistener object function. To listen for position information, set up a listener for the "MovingROI" or "ROIMoved" events.

Update Code

Update all instances of addNewPositionCallback.

Discouraged UsageRecommended Replacement

This example uses the addNewPositionCallback method to specify a callback function to execute when the ROI changes position. In this example, the callback function displays the current position in a title.

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

addNewPositionCallback(h,@(pos) myCallback(pos));

function myCallback(pos)



Here is equivalent code, replacing the addNewPositionCallback object function with the addlistener object function. This example listens for the "MovingROI" event.

h = drawrectangle(gca,"Position",[10 10 100 100]);

addlistener(h,"MovingROI",@(src,evt) myCallback(evt));

function myCallback(evt)