Drag and drop plot (inset) within another plot?
30 views (last 30 days)
Show older comments
Hi, sorry if this is really obvious!
I am trying to interactively move one plot within another figure using drag and drop mouse operations.
I am using the following function to create the interactive behaviour (below):
As you can hopefully see, it allows me to successfully move both a text box or the graph axis around a figure window succesfully and log x + y co-ordinates.
The function is based upon this answer here: https://uk.mathworks.com/matlabcentral/answers/94681-how-do-i-implement-drag-and-drop-functionality-in-matlab
What I am trying to achieve is to move the plot once it has an image stored within it.
I can make the plot contain an image easily using this answer/place a figure within a figure: https://uk.mathworks.com/matlabcentral/answers/60376-how-to-make-an-inset-of-matlab-figure-inside-the-figure#comment_654093
However, when I do so I lose the interactive element! This happens as soon as any content is added to the axis.
I think it may be because the axis and content are stored as seperate objects but I am unsure?
I am aware of the various functions on FEX, Moveit etc etc, however I am trying to do it in this manner as I need to plug this function into a reasonably heavy backend so am trying to avoid messing around making things into patches and so on.
Thanks in advance! And again, sorry if i've missed the obvious, been staring at this for awhile now...
function drag_drop
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
a = annotation('textbox','position',[0.2 0.2 0.2 0.2],'String','...','ButtonDownFcn',@dragObject);
a1 = axes('Position',[.7 .7 .2 .2], 'ButtonDownFcn', @dragObject)
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
disp(orPos)
end
end
end
0 Comments
Accepted Answer
Voss
on 22 Jun 2022
Try setting 'HitTest','off' for whatever objects you add to the axes. This will allow mouse clicks to pass through to the axes underneath (so that the axes ButtonDownFcn executes), rather than being captured by the new object.
6 Comments
Mike McCullough
on 25 Feb 2024
you might be a level 9 but your solution is worhless!
I tried 'HitTest','off' on the command line and got UNRECOGNIZED function???
Walter Roberson
on 25 Feb 2024
As demonstrated in the code examples posted, you need
set(HANDLE,'HitTest','off');
for appropriate value of HANDLE
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!