A couple of days ago, a reader of this blog has posted a comment asking for advise in enabling the user to dynamically set plot properties. While this can be done using the built-in inspect function, the reader correctly noted that this presents a list of numerous properties, most of which may not be very interesting for the casual user. So I wanted to use the opportunity to show an alternative mechanism that I have used in one of my applications and I think answers the need. It relies on a border-less button that is located right next to the plot axis origin, and when clicked, presents a simple plot line-style modification dialog window.
We start by creating a simple Java button (a
com.mathworks.mwswing.MJButton in this case) with the simple text ‘+’. The benefit of using
com.mathworks.mwswing.MJButton rather than the standard
MJButton extends, is that
MJButton added a FlyOverAppearance property to the standard
JButton. This is a very handy feature that enables to present a border-less button, except upon mouse hove, in which case a shadow border is displayed. This is exactly the effect used to highlight toolbar buttons upon mouse hover. To emphasize the button’s action, we set a HAND_CURSOR cursor whenever the mouse hovers over the button.
This button is then displayed onscreen using the built-in semi-documented javacomponent function, at the axes origin position. We set the button’s callback property to uisetlineprops, which was adapted from a File Exchange submission by the same name:
axesPos = get(hAxes,'pos'); btLinePropsCbStr = ['uisetlineprops(findall(' num2str(hAxes,99) ',''type'',''line''))']; btLinePropsPos = [axesPos(1:2)+0.003,0.03,0.03]; % Note: all the following code is just to have a specific cursor % ^^^^ (HAND_CURSOR) when hovering over the button... btLineprops = com.mathworks.mwswing.MJButton('+'); btLineprops.setBorder(); btLineprops.setBackground(java.awt.Color.white); btLineprops.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); btLineprops.setFlyOverAppearance(true); btLineprops.setToolTipText('Modify properties of plot lines'); [dummy,btContainer] = javacomponent(btLineprops,[0 0 1 1],hFig); %#ok set(btLineprops, 'ActionPerformedCallback',btLinePropsCbStr); set(btContainer, 'Units','Norm', 'Position',btLinePropsPos);
The benefit of using this simple trick is that the ‘+’ button is unobtrusive, and yet highly accessible. Of course, similar button can be used for a wide variety of callback functionalities, limited only by your imagination!