File Exchange

image thumbnail

Moveit2 - Move a graphical object with the mouse.

version 1.0.0.0 (1.67 KB) by Anders Brun
Move graphical objects with your mouse. Works for pcolor, plot, patches and perhaps more.

3 Downloads

Updated 16 Mar 2009

View License

This is really a small hack that I wanted to share just to inspire more people to make interactive Matlab graphics. Use it as is or use the code as a template to do more advanced stuff.

Here is how it works (for a 2-D patch):

% First make the patch ...
t = 0:2*pi/20:2*pi;
X = 3 + sin(t); Y = 2 + cos(t); Z = X*0;
h = patch(X,Y,Z,'g')
axis([-10 10 -10 10]);
% Then make it movable
moveit2(h);

It also works for e.g. pcolor and plot:

% Adjust data to fit hypothesis using the mouse ... :-)
a = plot(rand(10,10));
moveit2(a);

Cite As

Anders Brun (2021). Moveit2 - Move a graphical object with the mouse. (https://www.mathworks.com/matlabcentral/fileexchange/23304-moveit2-move-a-graphical-object-with-the-mouse), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

John Yoahann Daniel

MKL

Nikolaus Koopmann

slight edit with optional..
- prior button-down-callback that is restored after the move.
- callback to be fired after move.

% original code taken from Moveit2 (File Exchange)
%
% originalButtonDownFcn: use, to restore a prior button down callback
% funToDoAfterwards : callback to fire upon button release
function mouseMoveGraphicsObject(src,~,originalButtonDownFcn,funToDoAfterwards)
if nargin<3
originalButtonDownFcn = [];
else
validateattributes(originalButtonDownFcn,{'function_handle'},{'scalar'})
end
if nargin<4
funToDoAfterwards = [];
validateattributes(originalButtonDownFcn,{'funToDoAfterwards'},{'scalar'})
end

% Unpack gui object
gui = get(gcf,'UserData');
% Remove mouse pointer
set(gcf,'PointerShapeCData',nan(16,16));
set(gcf,'Pointer','custom');
% Set callbacks
gui.currenthandle = src;
thisfig = gcbf();
set(thisfig,'WindowButtonMotionFcn',@(h,e)movit());
set(thisfig,'WindowButtonUpFcn',@(h,e)stopmovit(src,originalButtonDownFcn,funToDoAfterwards));
% Store starting point of the object
gui.startpoint = get(gca,'CurrentPoint');
set(gui.currenthandle,'UserData',{get(gui.currenthandle,'XData') get(gui.currenthandle,'YData')});
% Store gui object
set(gcf,'UserData',gui);

function movit()
% Unpack gui object
gui = get(gcf,'UserData');
try
if isequal(gui.startpoint,[])
return
end
catch
end
% Do "smart" positioning of the object, relative to starting point...
pos = get(gca,'CurrentPoint')-gui.startpoint;
XYData = get(gui.currenthandle,'UserData');
set(gui.currenthandle,'XData',XYData{1} + pos(1,1));
% set(gui.currenthandle,'YData',XYData{2} + pos(1,2));
drawnow;
% Store gui object
set(gcf,'UserData',gui);
end

function stopmovit(MovedObject,originalButtonDownFcn,funToDoAfterwards)
% Clean up the evidence ...
thisfig = gcbf();
gui = get(gcf,'UserData');
set(gcf,'Pointer','arrow');
set(thisfig,'WindowButtonUpFcn','');
set(thisfig,'WindowButtonMotionFcn','');
drawnow;
set(gui.currenthandle,'UserData','');
set(gcf,'UserData',[]);
if contains(func2str(originalButtonDownFcn),mfilename)
return % only first time
end
MovedObject.ButtonDownFcn = originalButtonDownFcn;
feval(funToDoAfterwards)
end

end

Jim

Nice. I'm trying to do the same but for characters/strings, i.e. anything put in figure using text(X,Y,'string'). Any ideas?

Najjar Adib

Thank you Anders,
It works on my student MATLAB v R2015b; however, it did not serve the need.
In addition to moving, it is Zooming the axes, it stoped the previous implemented WindowButtonDwn (showing the Pointer (x,y) over the figure.
It is good to mention that it is a simple code taking advantage of the MATLAB resources and done with a trained software annotation publisher.
Regards.

Adam Boughrira

Works great and literally saved me the headache of "trying" to do it myself.
Can't thank you enough
Keep up the great work Anders!

Bo Hu

Cristian Acevedo

Kajal Raut

Kribashnee Dorasamy

The code works great. Thank you. I want to now apply it on 2D images. How does one work around this?

Currently I am trying to convert the image to a patch, however when displayed its not anything like the original. How does convert the image to the patch, so that when it displays on the screen it looks like the original?

Aldo Tamariz B

Brandon

This works great, Anders! Is there a way to return the new position of the graphical object?

Erick Villegas

Hi! I use your code as you post it but didn't work it say's:

Undefined function 'moveit2' for input arguments of
type 'double'.

Error in Moveimageexample (line 8)
moveit2(h);

Any help!!!

Jonathan

Gang Wang

Hi Anders, I am using Moveit2 and find this interactive tool is great!

For my curiosity, I produced a 3D object patch (triangulated) and input to the function.

It can be observed that the object translates on XY plane. But the shape of the object is no longer preserved.

I did debugging and observed that the size of 'Vertices' matrix under the gui.currenthandle is automatically changed after line 66:
set(gui.currenthandle,'XData',XYData{1} + pos(1,1));

In other words, if the size of 'X' or 'YData' is [3 by 100];
then the new vertices size will always be updated to [3*100 by 3].

I guess some private functions updates the 'Vertices' whenever the XYZData is changed. But I cannot find the mechanism behind this.

Jan

Sorry, too confusing typos.
If the original WindowButton-Motion/-Up functions are restored after your function has finished, I will rate it with 5 stars.

Jan

No, please do not use a global variable. The need of an obscure name is a warning already! I'd suggest using the ApplicationData of the object, see setappdata.
Itr would restore the original WindowButtonMotionUp functions after your function is finished.
H1-Line, comments, it works.

Anders Brun

thanks a lot for the 5 star... Just a comment, there are still things that can be simplified. In particular I think it would be possible to store state of the object in a smarter and even more invisible way, instead of using the UserData attributes. Perhaps a global variable with an obscure name would do it.

Jveer

5* for simplicity

Jveer

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!