Is it possible to program the mouse cursor selection box on figures in MATLAB?

15 visualizaciones (últimos 30 días)
Hello everyone,
I am basically trying to recreate the well-known property of mouse cursor selection boxes for a current MATLAB project.
When I plot a rectangle on a figure, I would like my program to be able to deform the rectangle exactly in the way those typical
selection boxes of the mouse are deformed, if I would click for example the bottom-right corner of the box with the mouse and
drag it on the figure plane.
For this, I have found a code with a similar function for lines and curves, it is found in this question:
And the fitting code for the problem is attached here as dragpoints.m .
Now, when I use the following input examples on the dragpoints-function:
x = [6 12 12 6 6];
y = [6 6 12 12 6];
dragpoints(x,y,0,20,0,20);
the plotted rectangle will not be deformed in the way a mouse cursor selection box would be when I move one corner with my mouse cursor.
So my question is: Is there a way I could change this code and make it deform rectangles like selection boxes?
Is there any way I can program this cursor selection box property in MATLAB in general?
Thanks in advance, I appreciate all your help!
  2 comentarios
Image Analyst
Image Analyst el 26 de Jun. de 2022
Not sure I understand what you want, but have you tried using drawrectangle to drag out a box and then using the coordinates from it to set xlim and ylim?
Muris Spahovic
Muris Spahovic el 26 de Jun. de 2022
Editada: Muris Spahovic el 26 de Jun. de 2022
Oh, um no actually. I didn't know this function exists, I will definitely check it out though.
Thank you very much! :)

Iniciar sesión para comentar.

Respuestas (1)

Ayush Kumar Jaiswal
Ayush Kumar Jaiswal el 26 de Jun. de 2022
Editada: Ayush Kumar Jaiswal el 26 de Jun. de 2022
I think here is what you need:
function dragpoints(xData,yData,xLower,xUpper,yLower,yUpper)
figure;
x = xData;
y = yData;
ax = axes('xlimmode','manual','ylimmode','manual');
ax.XLim = [xLower xUpper];
ax.YLim = [yLower yUpper];
%can change the marker size or marker type to make it more visible.
%Currently is set to small points at a size of 2 so is not very visible.
line(x,y,'marker','.','markersize',2,'hittest','on','buttondownfcn',@clickmarker)
function clickmarker(src,ev)
set(ancestor(src,'figure'),'windowbuttonmotionfcn',{@dragmarker,src})
set(ancestor(src,'figure'),'windowbuttonupfcn',@stopdragging)
function dragmarker(fig,ev,src)
%get current axes and coords
h1=gca;
coords=get(h1,'currentpoint');
%get all x and y data
x=h1.Children.XData;
y=h1.Children.YData;
%check which data point has the smallest distance to the dragged point
x_diff=abs(x-coords(1,1,1));
y_diff=abs(y-coords(1,2,1));
[value index]=min(x_diff+y_diff);
%create new x and y data and exchange coords for the dragged point
x_new=x;
y_new=y;
% Ensure that x and y moves in a coordinated manner
if (index == 1)
x_new(index)=coords(1,1,1);
x_new(end)=coords(1,1,1);
x_new(4) = coords(1,1,1);
y_new(index)=coords(1,2,1);
y_new(end)=coords(1,2,1);
y_new(2)=coords(1,2,1);
elseif index == 2
x_new(index)=coords(1,1,1);
x_new(3) = coords(1,1,1);
y_new(index)=coords(1,2,1);
y_new(1)=coords(1,2,1);
y_new(end)=coords(1,2,1);
elseif index == 3
x_new(index)=coords(1,1,1);
x_new(2) = coords(1,1,1);
y_new(index)=coords(1,2,1);
y_new(4)=coords(1,2,1);
elseif index == 4
x_new(index)=coords(1,1,1);
x_new(1) = coords(1,1,1);
x_new(end) = coords(1,1,1);
y_new(index)=coords(1,2,1);
y_new(3)=coords(1,2,1);
elseif index == 5
x_new(index)=coords(1,1,1);
x_new(1)=coords(1,1,1);
x_new(4) = coords(1,1,1);
y_new(index)=coords(1,2,1);
y_new(1)=coords(1,2,1);
y_new(2)=coords(1,2,1);
end
%update plot
set(src,'xdata',x_new,'ydata',y_new);
function stopdragging(fig,ev)
set(fig,'windowbuttonmotionfcn','')
set(fig,'windowbuttonupfcn','')
  1 comentario
Muris Spahovic
Muris Spahovic el 26 de Jun. de 2022
Editada: Muris Spahovic el 26 de Jun. de 2022
Just tested your function, it works very well, although the rectangle is sometimes a bit shifted when
it's deformed. But the function is definitely what I was looking for, this will definitely help me further
on my project!
Thank you very much Ayush Kumar!

Iniciar sesión para comentar.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by