File Exchange

image thumbnail


version 1.3 (11.5 KB) by

Allows graphical objects to be dragged in a figure.



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function enables a graphical object (be it a line, plot, patch, rectangle, text box, etc.) to be dragged inside an axis, with a very simple syntax ("draggable(h)"), following or not horizontal, vertical or diagonal constraints. Limits can be imposed on the object's movement.

Some figure properties are changed by the function, but previous properties are restored as soon as the drag is stopped. The behavior of the object can be reverted to its original, non-draggable state, by issuing "draggable(h,'off')".

Typical uses of this function would include non-standard GUI elements, such as markers that are moved on a figure or image. A function which is called when the object is moved can be provided as an optional argument, so that the movement triggers further actions.

See the provided file dragdemo.m for usage examples.

Comments and Ratings (33)

Chaoyu Zhang

Gabriel Marsh


Jess (view profile)



Aldi Wijaya

Excellent Work, thanks Francois

Yang Yu

Armindo: this is possible by changing the axes limits (using xlim and ylim) inside the callback function provided as a parameter. I'm trying to make a demo but doing it so that it's not confusing to the user (e.g. axes panning too fast) requires care.


Very nice...

would be possible to drag an object horizontally while updating the axes 'xlim' property.

Basically I need to be able to drag a patch and while iam doing that when Iam near de limits of the plot the plot adjust the limits so I can still be able to drag...

Can you help me on this please

Sergei P.


Manuel (view profile)


liu (view profile)


Massimo Ciacci

Massimo Ciacci (view profile)

Well written, and plenty of demos... I am speechless, exactly what i was looking for!

More precisely I loved how accurate and concise are both the demo code and the function itself! Well done!


Scott (view profile)

I had to create an account and a community profile to rate this. This is simply awesome! Thank you Francois!

Timo Dörsam

Yi Sui

Yi Sui (view profile)

This function allows me to place comment boxes on the plot and manually adjust them before inserting in presentation


Shahab (view profile)

Excellent, tahnks

Shaun's problem should be fixed now; I instead simply used fgh = gcbf; -- it should work in all situations. Thanks to Esmerald Aliai for pointing out a similar bug.


Shaun (view profile)

I love it, and I use it all the time.
One mod I have made for axes which live inside UICONTAINERS...

fgh = get(axh,'Parent');
while ~strcmp(get(fgh,'Type'),'figure')
fgh = get(fgh,'Parent');

Sorry for the years-long delay since the last update... The Jan 13 update contains a bug fix by G. Fortin and Steven Bierer's drag-end function call. I'll try to add Brett's suggestion shortly.

Brett Shoelson

Brett Shoelson (view profile)

Most excellent work...rock solid!

Steven Bierer

An excellent, deviously simple tool. The code is well commented, and I was easily able to modify it to include a new input argument: a function handle that executes AFTER the object has been dragged. (I will contact the author with this addition, in case he'd like to incorporate it.)

I used it to move text, and it worked well after adapting your code to objects having a "Position" property of 3 values.

Nathan Tomlin

Wow, amazingly well done!

Chris Sarantos

Excellent, just what I was looking for!

Paul Premakumar

Very handy file!

Tom Nagel

I love it because it learns me a lot about mousebutton handling.
I tried to make a text object draggable: that doesn't work!

w L

Laszlo Balkay

It is a really great work with impressive options.


Ohad Gal

Well done.
you might want to add an option for the following:
1. force a movement of the object, using an outside calling function.
2. enable the user to choose it's rendering mode
3. export (and update a given edit box) the DX and DY movement of the object

anyways, It's great. thanks you !




This update fixes a bug found by Esmerald Aliai in which draggable would not work for axes embedded in Panels. In fact this should also fix Shaun's bug mentioned in March 2012.


Major update. Now supports text objects. Diagonal constraints with arbitrary slopes added. Single call for applying draggable on multiple objects sharing the same parameters supported. Added the 'sliders' demo in dragdemo. General cleanup.


implemented a feature and fixed a bug (both user-suggested)

- Bugfix: yet another correction to movement computation. Now behaves correctly when mouse is out of axes.

- Bugfix: sanitized movement computation.
- The 'snapgrid' demo has been simplified.
- The 'polymove' demo has been polished.

- Bugfix: now works with 1-element plot and line objects
- Added: polygon with draggable vertices in as demo 'polymove' in dragdemo

- Changed the category from "GUI tools and examples" to more accurate "Graphics: Graphical Data Exploration and Interaction".
- Now distributing .zip archive containing both draggable.m and a demo function, dragdemo.m.

Changed the category from "GUI tools and examples" to more accurate "Graphics: Graphical Data Exploration and Interaction".

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today