Borrar filtros
Borrar filtros

Drag and drop plot (inset) within another plot?

15 visualizaciones (últimos 30 días)
Michael Vinsome
Michael Vinsome el 22 de Jun. de 2022
Comentada: Walter Roberson el 25 de Feb. de 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

Respuesta aceptada

Voss
Voss el 22 de Jun. de 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 comentarios
Mike McCullough
Mike McCullough el 25 de Feb. de 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 el 25 de Feb. de 2024
As demonstrated in the code examples posted, you need
set(HANDLE,'HitTest','off');
for appropriate value of HANDLE

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by