image thumbnail

draggableRectangle

version 1.0.4 (3.95 KB) by Hyeokjin Jho
Draws rectangle which can be dragged or resized by mouse pointer

30 Downloads

Updated 16 Dec 2020

View License

Draws rectangle which can be dragged or resized by mouse pointer

D = draggableRectangle (parent_axes,xrange,yrange,...);
input
parent_axes : axes object : axes for drawing rectangle
xrange : 1 x 2 numeric : [xmin xmax]
yrange : 1 x 2 numeric : [ymin ymax]

name-value pairs
xdirlock : logical scalar : lock x direction drag
ydirlock : logical scalar : lock y direction drag
buttonDownFcn : function handle : callback executed upon mouse click
buttonDragFcn : function handle : callback executed upon mouse drag movement
buttonUpFcn : function handle : callback executed upon mouse release

additional name-value pair argument for 'patch' can be followed after these arguments (see:https://www.mathworks.com/help/matlab/ref/matlab.graphics.primitive.patch-properties.html)

properties
xrange : 1 x 2 numeric : current x range of rectangle
yrange : 1 x 2 numeric : current y range of rectangle
xdirlock : logical scalar : if true, locks x dir dragging
ydirlock : logical scalar : if true, locks y dir dragging
bezel : numeric positive scalar : determines width of edge dragging region
isdragging : logical scalar (read only) : true if object under dragging
buttonDownFcn(hObject,eventdata) : function handle : function executed when mouse is clicked on this object
buttonDragFcn(hObject,eventdata) : function handle : function executed when mouse is moved during drag
buttonUpFcn(hObject,eventdata) : function handle : function executed when drag is finished

Additional properties for 'patch' are available.
Standard get & set functions are available.

methods
D.drag_start([movingx,movingy]);

example
% draw axis
axis([0 40 0 60]);
% draw rectangle
D = draggableRectangle([10 30],[20 40],'facecolor','r');
% force start dragging
D.drag_start([-1,0]) % move left edge

% wait until dragging is done
while D.isdragging
pause(0.05)
end

% Here, we change rectangle's properties (same properties as 'patch')
D.FaceColor = 'y';
set(D,'EdgeColor','none');

% force start dragging
D.drag_start([1,1]) % move top right vertex

% wait until dragging is done
while D.isdragging
pause(0.05)
end

% Here, we define user callback
% force start dragging
D.drag_start([0,0]) % move position
% set mouse release callback
D.buttonUpFcn = @(h,~) fprintf('moved position : [%.2f %.2f %.2f %.2f]\n',h.xrange,h.yrange);

Cite As

Hyeokjin Jho (2022). draggableRectangle (https://www.mathworks.com/matlabcentral/fileexchange/84370-draggablerectangle), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020b
Compatible with R2019b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!