Main Content


Remove new-position callback from ROI object

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



removeNewPositionCallback(h,id) removes the corresponding function from the new-position callback list of the ROI object, h.


collapse all

Create a line ROI object. Display the position of the line in the title. Use addNewPositionCallback to update the title each time you move the line.

h = imline(gca,[10 100],[100 100]);
id = addNewPositionCallback(h,@(pos) title(mat2str(pos,3)));

Move the line to observe the callback behavior.

After observing the callback behavior, remove the callback. The title no longer changes when you move the line.


Input Arguments

collapse all

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

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

Version History

Introduced in R2008a

collapse all

R2018b: removeNewPositionCallback 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 remove this callback, delete the listener object.

Update Code

Update all instances of removeNewPositionCallback.

Discouraged UsageRecommended Replacement

This example uses the addNewPositionCallback method to specify a callback function to execute when the ROI changes position. The code then uses removeNewPositionCallback to remove the callback.

h = imrect(gca, [10 10 100 100]);
% Add callback that updates the title with position.
id = addNewPositionCallback(h,@(p) title(mat2str(p,3)));
% Remove position callback. Title no longer updates.

Here is equivalent code, creating a new ROI object and replacing the addNewPositionCallback object function with the addlistener object function. This example listens for the "MovingROI" event. To remove the listener, use delete(el).

h = drawrectangle(gca,"Position",[10 10 100 100]);
% Set up a listener for ROI moving events.
el = addlistener(h,"MovingROI",@mymovecb)
% Callback to update title with current position
function mymovecb(src,evt)
    currpos = evt.CurrentPosition;