Code covered by the BSD License  

Highlights from
draggableRect

5.0

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

draggableRect

by

 

04 Apr 2012 (Updated )

Create a draggable rectangle in normalized units.

| Watch this File

File Information
Description

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.

Acknowledgements

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

@Brett,

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

Cheers

27 May 2014 Brett Shoelson

@Eric,
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,...
'units','normalized',...
'position',[0.05 0.05 0.9 0.9],...
'xlimmode','manual',...
'ylimmode','manual');

% 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,...
'units','normalized',...
'position',get(ax,'position'),...
'xlimmode','manual',...
'ylimmode','manual',...
'color','none',...
'xtick',[],...
'ytick',[]);
% call draggablerect
draggableRect(false,gridAx);

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.
Cheers,
Brett

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],...
'xlimmode','manual','ylimmode','manual');
% plot map using imagesc
map_im = ones(100,100);
imagesc(map_im, 'Parent', gridAx)
% call draggablerect
draggableRect(false,gridAx);

Updates
04 Jun 2014

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