MATLAB: Does “ginput” return points the left axes generated by “plotyy” even though I have set the right axes as the current axes

MATLAB

I created a plot using "plotyy".  I would then like to select points using "ginput" from the right-plot, but any time I select a point in the axes I get information regarding that point as if I had clicked on the left axes.  I have tried making the right axes the current axes, but this had not helped.  Is there a way to use tell "ginput" to select the points from the right axes?

Best Answer

This is expected behavior.  The reason "ginput" is providing you with information from the left axes even when you have set the right axes as the current axes is because of the way the axes properties are set by "plotyy";  "plotyy" sets the right axes as not-selectable.  A workaround to make sure that the right axes is used instead of the left axes when selecting it with "ginput" is to include the following two commands:
 
    >> set(Ax(2),'HitTest','on')
    >> set(Ax(1),'HitTest','off')
 
This will make the right axes selectable and left axes not selectable when using "ginput".  Below is a more detailed explanation of what causes this expected behavior.
 
The "plotyy" function superimposes two axes on top of each other so as to display two different pieces of information. In doing so, it determines that the left axes will be the ones to be display on top of the right axes.  In order for this to happen, it sets the 'HitTest' property of the left axes to 'on', while that of the right axes to 'off'.  This property determines if the axes is selectable by the mouse click.  As you can see, setting the "background" axes 'HitTest' property to 'off' makes sure the axes cannot be selected by the mouse.  Here is a link to the documentation that talks about the different properties for an axes.  There you will find that for 'HitTest':
 
     http://www.mathworks.com/help/releases/R2014a/matlab/ref/axes_props.html
 
The result of these settings mean that when you click on a particular location using "ginput" you are actually clicking on the left axes even though you have told MATLAB that the right axes is the current one.  You can also verify this via two methods:
 
1) You can get the 'HitTest' property of each of the axes and identify that the right axes is set to 'off', while that of the left axes is set to 'on'.
 
2) You can use "ginput" to select on the red plot line that corresponds to the right axes and you will see the result is a y-value in the righ axes coordinates.  This is because although the right axes 'HitTest' property is 'off', that same property for its plot line is set to 'on'.  In other words, although the axes is not selectable the plot line itself is.