Hello, I want ask how put a mouse click to define a point in space with a window size [min1, max1] and [min2, max2]?
MATLAB: Mouse click in a particle Swarm optimization
mouse click
Related Solutions
The ability to click and drag corners or points and move objects around within a figure is not available in MATLAB. Such features of using the mouse to move data points are not possible.
As a workaround, try the user-contributed code, MOVEPLOT, which can be found on the MATLAB Central File Exchange:
You may also want to consider the user-contributed code, MOVEPLOT2:
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.
Alternatively, you can use the following example as a guide to developing your own routine for moving points:
First make sure that the following two functions are on your MATLAB path:
rect.m animator.m
Then you can use the following function code.
function rect(x)if nargin <1 x = 'start';endswitch xcase 'start' figure; axes('nextplot','replacechildren') axis([0 5 0 5]) x = [0 1 1 0 0]; y = [0 0 1 1 0]; l1 = line(x,y); set(l1,'Tag','Line1') set(l1,'Buttondownfcn','animator(''start'')') set(gca,'ButtonDownFcn','rect(''gcf_bdf'')')case 'gcf_bdf' l1 = findobj('tag','Line1'); % set(l1,'Selected','on')
A = get(gca,'CurrentPoint'); X = A(1,1); Y = A(1,2); XData = get(l1,'Xdata'); YData = get(l1,'Ydata'); [dX,Ix] = min(abs(XData-X)); [dY,Iy] = min(abs(YData-Y)); FinalX = X-XData(Ix); FinalY = Y-YData(Iy); XData = XData + FinalX; YData = YData + FinalY; set(l1,'Xdata',XData) set(l1,'Ydata',YData)endfunction animator(action)switch(action)case 'start', set(gcbf,'WindowButtonMotionFcn','animator move') set(gcbf,'WindowButtonUpFcn','animator stop')case 'move' l1 = findobj('tag','Line1'); set(l1,'Selected','on') XData = get(l1,'Xdata'); YData = get(l1,'Ydata'); currPt=get(gca,'CurrentPoint'); X = currPt(1,1); Y = currPt(1,2); FinalX = X-XData(3); FinalY = Y-YData(3); XData(2:3) = XData(2:3) + FinalX; YData(3:4) = YData(3:4) + FinalY; set(l1,'Xdata',XData) set(l1,'Ydata',YData)case 'stop' set(gcbf,'WindowButtonMotionFcn','') set(gcbf,'WindowButtonUpFcn','') l1 = findobj('tag','Line1'); set(l1,'selected','off')end
There are two properties which can be used in conjunction to accomplish this.
One is the button's 'ButtonDownFcn' callback function. This callback function executes when pressing a mouse button on or near a UICONTROL object -- including when pressing the right mouse button.
The other property is the figure's 'SelectionType' property. This property indicates which kind of click was registered in the figure window -- including clicks on controls within the figure.
Putting these two together, you can define a 'ButtonDownFcn' callback for a push button which checks the figure's 'SelectionType' property to detect a right-click. An example is shown below. (In that example, the ANCESTOR function is used to get the figure's handle. If this is being done in a GUIDE-created GUI, this is unnecessary as the 'handles' structure already provides access to the figure's handle.)
function test uicontrol('Style', 'pushbutton', ... 'ButtonDownFcn', @myCallback);endfunction myCallback(src, evt) figHandle = ancestor(src, 'figure'); clickType = get(figHandle, 'SelectionType'); if strcmp(clickType, 'alt') disp('right click action goes here!'); endend
Best Answer