Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Borderless button used for plot properties

May 11, 2011 16 Comments

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 javax.swing.JButton, which 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);

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);

A borderless button used to modify plot properties
A borderless button used to modify plot properties

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!

Related posts:

  1. Plot LimInclude properties – The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....
  2. Toolbar button labels – GUI toolbar button labels can easily be set and customized using underlying Java components. ...
  3. Undocumented button highlighting – Matlab button uicontrols can easily be highlighted by simply setting their Value property. ...
  4. Adding custom properties to GUI objects – It is very easy to add custom user-defined properties and methods to GUI handles and Java references in Matlab. ...
  5. Button customization – Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways that are impossible using pure Matlab....
  6. Plot-type selection components – Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...
GUI IDS Java Semi-documented function Undocumented feature
Print Print
« Previous
Next »
16 Responses
  1. Jason D. May 20, 2011 at 08:18 Reply

    This looks very useful! Thank you.

    Now, to figure out how to expand this to modify properties for several plots on one set of axes (but that’s the fun part).

  2. Aurélien May 24, 2011 at 05:42 Reply

    Thanks Yair for sharing this feature. As you said the ‘+’ button is unobtrusive compared to any other uicontrol.

  3. xto May 26, 2011 at 10:01 Reply

    Looks like a very handy feature. Actually (being a newbie in matlab programming) I’ve some problems in setting your code snippet into a working code frame. How do I obtain hAxis and hFig ?
    Thanks for giving more details …
    Rudi

    • Yair Altman May 26, 2011 at 10:08 Reply

      @Rudi – hFig is simply the Matlab handle for the containing figure, and hAxis is the handle of the relevant plot axis. These handles can be gotten via the gcf and gca functions respectively.

      • xto May 27, 2011 at 04:29

        thanks. That helped!

        So a working sample (on top of uisetlineprop) would be:

        s=peaks;
        handle=plot(s(:,25)); 
        axesPos = get(gca,'pos');
        btLinePropsCbStr = ['uisetlineprops(findall(' num2str(gca,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],gcf); 
        set(btLineprops, 'ActionPerformedCallback',btLinePropsCbStr);
        set(btContainer, 'Units','Norm', 'Position',btLinePropsPos);

        s=peaks; handle=plot(s(:,25)); axesPos = get(gca,'pos'); btLinePropsCbStr = ['uisetlineprops(findall(' num2str(gca,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],gcf); set(btLineprops, 'ActionPerformedCallback',btLinePropsCbStr); set(btContainer, 'Units','Norm', 'Position',btLinePropsPos);

  4. Joel June 17, 2011 at 13:32 Reply

    Is there anyway to set a uicontrol pushbutton to be borderless?
    I like what you did, do you know how to do that using uicomponent(‘style’, ‘pushbotton’) instead of using a java button?

    • Yair Altman June 19, 2011 at 03:07 Reply

      @Joel – customization of uicontrol buttons, including the issue of changing or removing the border, was discussed here.

  5. James Kapaldo July 22, 2012 at 17:40 Reply

    Is there a way to do this same thing, nicely, for a 3D plot. What I want is one button on either end of the xAxis, such that when you press one of the buttons the xAxis limits change. (This would be one solution for zooming 3d graphs in the x-axis direction; since, the normal zoom function does not have proper properties for use in GUIs.)
    Placing the buttons so that they stay in the correct position when rotating the 3D axis seems to be problematic, however. I have been trying to use viewmtx or the hidden properties such as x_ViewTransform in conjugation with the dsxy2figxy from the FEX, but the buttons still do not appear in the correct locations.

    • Yair Altman July 22, 2012 at 22:39 Reply

      @James – I do not know any easy way to do it. You can place a property listener on the aforementioned axes properties, such that whenever these properties change, the invoked callback will update the button position accordingly.

  6. James Kapaldo July 23, 2012 at 03:26 Reply

    Yes, that is what I was using. So, there is no hidden property or some easy way to get the figure pixel that corresponds to a point in a 3D axes? Or, more so, to get the pixel in the axes bounding box that corresponds to the 3d point? I think it should just be some affine transformation; however, I am unsure of where the axes bounding box lies in relation to the camera or the 3d axes. (I apologize for re-iterating the question; however, I saw that I never actually asked concisely in the previous post and want to be sure I was understood.)

    • Yair Altman July 29, 2012 at 03:40 Reply

      @James – you can get the axes pixel positions via the properties described here. You should also take into consideration the DataAspectRatio and related properties, described here. Remember to ensure that the axes uses pixel Units, and also to take into account that the axes may possibly be a child of some figure container (e.g., uipanel, uitab etc.), which have their own pixel position vector. And yes – after you take all these into consideration the rest is just a simple affine transformation…

  7. Color selection components | Undocumented Matlab October 16, 2013 at 03:33 Reply

    […] An example of such an integrated control can be found in the uisetlineprops utility on the File Exchange, which I introduced earlier this month in my article on using a borderless button for setting plot properties: […]

  8. Mathieu G August 28, 2015 at 07:34 Reply

    Hello!

    Thanks for your post. It is very helpful!
    I don’t know the java language but I try to modify a button of my GUI (using GUIDE) to a borderless button. So I tried to use the setBorder function with [] to signify empty borders, but it is not working. I succeeded using the Fly Over Appearance in the create function of my button, like this:

    variable = findjobj(hObject);
    variable.setFlyOverAppearance(true);

    variable = findjobj(hObject); variable.setFlyOverAppearance(true);

    But when I open my GUI, this one opens, then is hidden by Matlab and re-appears after. It seems to be a double opening on the screen.
    Do you know why?

    Mathieu G.

    • Yair Altman August 28, 2015 at 10:31 Reply

      @Mathieu – try to run findjobj after your figure is made visible if you wish to avoid the flicker

      • Mathieu G September 3, 2015 at 05:35

        Hi Yair,
        What do you mean by Visible? I am a beginner with Guide; for me, the create function and then the opening function of the figure are called by Matlab when I run the gui with the Command Window. I try to add my lines (variable = findjobj(findobj(gcf, 'Tag', 'bouton11'); variable.setFlyOverAppearance(true);) in these functions before your answer and also in the create function of the button but there is always this flicker.
        I feel i am not far from the solution but I have no more ideas.

        I thank you in advance.

      • Mathieu G September 3, 2015 at 05:42

        Ok I found the solution. It is just because I didn’t know the output function I discovered few seconds ago on another page of your wonderful website.

        Thanks a lot for your work!

        Another question if I can permit :
        Is it possible to do button with round corner easily?

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top