Dragging an axes with a mouse within a figure: Matlab Gui Guide

19 views (last 30 days)
The purpose of the class is to allow the user to click on an image(i.e axes) and drag it across the figure. When a mouse click is detected, the function detects which axes the mouse location lies within and initiates 'windowbuttonmotionfcn' which calls up a function 'movit'. The purpose of function 'movit' is to drag the axes selected as the mouse moves while the mouse button is down.
function Mclicked(this, src, event)
% get location of mouse click on the gui
set(gca,'units','pix') ;
mousePositionData = get(gca, 'CurrentPoint')
this.x = mousePositionData(1,1);
this.y = mousePositionData(1,2);
%get origin position of all axes within the figure
set(gca,'units','pix') ;
AxesHandle=findobj(gcf,'Type','axes');
pt1 = get(AxesHandle,{'Position','tightinset'}); % [left bottom right top]
% ... ##get the axes that mouse as clicked on and store it in Values as a mat
set(gcf,'windowbuttonmotionfcn',@( src, event) movit(this,src, event));
set(gcf,'windowbuttonupfcn',@( src, event) stopmovit(this, src, event));
end
end
I stored the original position of the mouse when it was first clicked in variable x and y. The algorithm below gets the new position of the mouse when the button is down and calculates the difference/distance between these two mouse movements. This difference is added to get the new position of the axes.
function movit(this, src, event)
%get location of new mouse position on the gui
set(gca,'units','pix') ;
mousePositionData = get(gca, 'CurrentPoint')
this.x2 = mousePositionData(1,1);
this.y2 = mousePositionData(1,2);
this.distancex= this.x2-this.x;
this.distancey= this.y2-this.y;
%get the new location of the image.
this.x2=this.Values(1,1)+this.distancex;
this.y2=this.Values(1,2)+this.distancey;
set(gca,'Position',[this.x2 this.y2 this.h this.w]); %x y h w
drawnow;
end
The problem I am experiencing is that the axes does not move adjacent to the mouse. For example, when the mouse button is down and the mouse is moving down or even up, the image/axes moves downwards and disappears. It does not move alongside the mouse cursor.
I did a test to verify if the set(gca,'Position',[...]); %x y h w is working correctly by moving across the figure using a counter which I increased by 1 and added the value to the original position. The axes moved as expected and visible to the user.Therefore, set(gca,'Position',[...]); %x y h wworks fine. However, I am unsure what the error is . I assume its related to the calculations or a piece of code that I suppose to call up.

Accepted Answer

Josh
Josh on 28 Feb 2017
I think you want to get the figure's current point and not the axes' current point when calculating your scaling. Getting the axes current point grabs a point in the axes; you want the pixel value of the current point relative to the figure origin, which is what get(gca, 'CurrentPoint') will get you. Figure's current point does have some limitations to when it updates (see Figure Properties in the MATLAB documentation). An alternative is to do get(0, 'PointerLocation') (this gives the position of the pointer on the screen) and subtract off the position of the figure (the first two elements in get(gcf, 'Position')).

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!