0

In MATLAB 2014b, using the brush tool on the figure, I can create a rectangular region of interest. The coordinate X and Y are updated while holding the left-click button of the mouse. When I release the mouse, is it possible to retrieve the last X and Y coordinates data displayed on the plot ?

enter image description here

oro777
  • 1,110
  • 1
  • 14
  • 29

1 Answers1

2

Have a look at the example below which shows you a way of doing it using a listener to the mouse button release action.

update: based on comment OP wants the start and end points.

Added a listener to the mouse being pressed which stores those points in the axes user data and then this is displayed along with the co-ordinates when the mouse button is released.

function display_coordinates_example
  % create a figure
  f = figure;
  % create an axes
  ax = axes;
  % plot your data
  plot ( 1, 1, 'o');

  % add a listener to be activated when the mouse button is released.
  addlistener ( f, 'WindowMousePress', @(a,b)mousepress(ax) );
  addlistener ( f, 'WindowMouseRelease', @(a,b)mouserelease(ax) );
end
function mousepress ( ax )
  ax.UserData.cp = ax.CurrentPoint(1,1:2);
end
function mouserelease ( ax )
  % display the current X,Y position.
  startCp = ax.UserData.cp;
  cp = ax.CurrentPoint(1,1:2);
  fprintf ( 'X = [%f, %f] and Y = [%f, %f]\n', startCp(1,1), cp(1,1), startCp(1,2), cp(1,2) )
end

Update 2

If you weren't using brush then yes you could use the standard Matlab callbacks for the figure, e.g.

set ( f, 'WindowButtonDownFcn', @(a,b)mousepress(ax) );
set ( f, 'WindowButtonUpFcn', @(a,b)mouserelease(ax) );

Try it - before you brush the data and you will see that it works. But when you brush data Matlab uses these callbacks for the brushing -> so they are temporarily disabled... To get around this problem you have to use listeners to the mouse press and release.

The systax @(a,b)mousepress(ax) could be replaced with a more standard callback:

addlistener ( f, 'WindowMousePress', @mousepress );

Using this syntax in the callback you would then have to find the axes handle, as the inputs by default would be the figure handle f and the mouse event info -

function mousepress ( f, event )
  ax = findobj ( f, 'type', 'axes' );
  ax.UserData.cp = ax.CurrentPoint(1,1:2);
end

findobj is a waste of time in my view - you have the handle when its created so lets keep it rather than finding it later when we need it.

What I did instead is to pass the item on interest (ax) into the callback and not pass in the other items that I am not interested in. I do this by creating an anonymous function. The a,b represent the normal callback inputs fig and event data, but I do not pass them into the callback function - instead I pass in the variable of interest - ax.

matlabgui
  • 5,642
  • 1
  • 12
  • 15
  • Thank you for the sample program but ' CurrentPoint returns the current point x and y data. I would like to get the data shown on the plot, for example in my uploaded image : X = [0.606, 1.53] and Y = [1.68, 0.516] – oro777 Dec 10 '15 at 08:58
  • Thanks for the update. I am not familiar with addlistener(), is it possible to do the same with set(f, 'WindowMousePress', @mousepress(ax)) ? Also could you tell me what is the meaning of a and b in the following code : @(a, b)mousepress(ax) ? – oro777 Dec 11 '15 at 02:39
  • Thanks for the update. I did not try but does the following syntax works ? I use it for normal callback with arguments : set( f, 'WindowMousePress', {@mousepress, ax} ). In the mousepress function, can I get ax using varargin{3} ? – oro777 Dec 18 '15 at 02:02