Effacer les filtres
Effacer les filtres

Drag and drop plot (inset) within another plot?

15 vues (au cours des 30 derniers jours)
Michael Vinsome
Michael Vinsome le 22 Juin 2022
Commenté : Walter Roberson le 25 Fév 2024
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.
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

Réponse acceptée

Voss
Voss le 22 Juin 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 commentaires
Mike McCullough
Mike McCullough le 25 Fév 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
Walter Roberson le 25 Fév 2024
As demonstrated in the code examples posted, you need
set(HANDLE,'HitTest','off');
for appropriate value of HANDLE

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by