Code covered by the BSD License  

Highlights from

5.0 | 1 rating Rate this file 18 Downloads (last 30 days) File Size: 49.6 KB File ID: #35895 Version: 1.1
image thumbnail



Brett Shoelson (view profile)


04 Apr 2012 (Updated )

Create a draggable rectangle in normalized units.

| Watch this File

File Information

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.


Draggable inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
28 May 2014 Eric Deal


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


Comment only
27 May 2014 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.

Comment only
27 May 2014 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

04 Jun 2014 1.1

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).

Contact us