File Exchange

image thumbnail


version (49.6 KB) by

Create a draggable rectangle in normalized units.

1 Download


View License

Like MATLAB's dragrect function, this function allows you to interactively define a draggable rectangle. However, UNLIKE MATLAB's function, this version works in normalized units, and it includes SNAP-TO behavior to constrain the rectangle to a user-specified grid.
Syntactically, the function is implemented to use Parameter-Value pairs, and allows the user to specify a non-interactive starting point, arrays of x- and y- points for snap-to behavior, and the axis you want to draw in. Outputs variables can contain a handle to the created rectangle, as well as an array of handles to the draggable perimeter points. (Output points are in the order: [BL,BR,TL,TR,BM,TM,RM,LM]).
Now accepts a "new position callback function."

Thanks to Francois Bouffard for his excellent implementation of "draggable," which greatly facilitated this entry.

Comments and Ratings (3)

Eric Deal


Thanks a ton. It worked perfectly. I never figured that out on my own.


Brett Shoelson

Brett Shoelson (view profile)

Thanks very much for the rating and comments. It took me a while to figure out what's going on here. I wrote this to work entirely in normalized units. When you display an image (imagesc) in the same axes as the one that contains the draggableRect, it sets the x- and y- limits outside of [0,1], wreaking havoc with my engine.

As a workaround, consider putting the image in a separate axes. For instance:

% create figure handle
mapfig = figure(1);
ax = axes('parent', mapfig,...
   'position',[0.05 0.05 0.9 0.9],...

% plot map using imagesc
map_im = ones(100,100);
imagesc(map_im, 'Parent', ax)

% Now create a separate [01] axes
% for the draggableRect:
gridAx = axes('parent',mapfig,...
% call draggablerect

Notice that I explicitly set the color of the overlaying axes to 'none', and toyed with some of the axes settings to try to give you what you're after.

Let me know if that works for you.

Eric Deal

Hello Brett,

I really like this function. Very cool and sleek looking. It does exactly what I need, almost. I find that when I try to use this function with imagesc, it will draw the rectangle perfectly fine, but afterwards if you try to move the rectangle by clicking on it, it seems to shoot up into the upper left corner and become tiny. I was wondering if you had insight into why this is happening. Here is the script I am using:

% create figure handle
mapfig = figure(1);

hold on
% set axes
gridAx = axes('parent', mapfig,'units', 'normalized', 'position', [0.05 0.05 0.9 0.9],...
% plot map using imagesc
map_im = ones(100,100);
imagesc(map_im, 'Parent', gridAx)
% call draggablerect


Updated license


Modified to allow specification of new position callback function. Also added new example to show how to use draggableRect on top of an image axes (for example).

MATLAB Release
MATLAB 7.13 (R2011b)

Inspired by: draggable

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

» Watch video