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

Panel-level uicontrols

November 24, 2010 17 Comments

In one of my larger Matlab applications – Integration-lab Debriefing System (IDS, which shall be described in a future dedicated article) – I wanted to present a panel-level checkbox that applies to the entire panel contents. In my particular case, the IDS uipanel contained a Java table (another future topic) with multiple selectable rows, and I needed a global checkbox that selects all (or none) of them at once:

Panel-level ("Objects") checkbox
Panel-level ("Objects") checkbox

One way to do this is to calculate the checkbox’s desired position relative to the uipanel and place a regular checkbox-style uicontrol there. The checkbox can even be made a child of the uipanel with ‘normalized’ units, thereby moving and resizing it together with its uipanel parent when the later is moved or resized.
But there’s a much simpler method that want to share. It relies on the undocumented fact that the uipanel‘s title label is a simple hidden uicontrol child of the uipanel handle. This uicontrol handle can be found and simply transformed from a ‘style’=’text’ control into a ‘style’=’checkbox’ control, as follows:

% Prepare the panel
hPanel = uipanel('position',[0.2,0.2,0.4,0.4], 'title','Objects');
% Get the title label's handle
warning off MATLAB:Uipanel:HiddenImplementation  % turn off warning
hTitle = setdiff(findall(hPanel),hPanel);  % retrieve title handle
hTitle = get(hPanel,'TitleHandle');  % alternative, uses hidden prop
% Modify the uicontrol style; add 20 pixel space for the checkbox
newPos = get(hTitle,'position') + [0,0,20,0];  % in pixels
set(hTitle, 'style','checkbox', 'value',1, 'pos',newPos);

% Prepare the panel hPanel = uipanel('position',[0.2,0.2,0.4,0.4], 'title','Objects'); % Get the title label's handle warning off MATLAB:Uipanel:HiddenImplementation % turn off warning hTitle = setdiff(findall(hPanel),hPanel); % retrieve title handle hTitle = get(hPanel,'TitleHandle'); % alternative, uses hidden prop % Modify the uicontrol style; add 20 pixel space for the checkbox newPos = get(hTitle,'position') + [0,0,20,0]; % in pixels set(hTitle, 'style','checkbox', 'value',1, 'pos',newPos);

Panel-level checkbox
Panel-level checkbox

Note that we can retrieve the title handle using either the uipanel‘s hidden property TitleHandle, or by scanning the panel’s children using findall. I prefer the TitleHandle approach because it does not require modification (minor as it might be) when the panel already contains other children.
The down-side is that since TitleHandle is an undocumented hidden property, it may change its name, its behavior or even be removed in some future Matlab release. In fact, there’s even a Matlab warning about this, unless you turn it off using:

warning off MATLAB:Uipanel:HiddenImplementation

warning off MATLAB:Uipanel:HiddenImplementation

We can use this approach for more complex options panels, as the following example illustrates. Here, we set radio-button controls rather than a checkbox control, and also modify the title color to blue:

% Main panel and basic alternative control
hPanelMain = uipanel('pos',[.1,.1,.8,.8], 'Title','Main', 'units','norm');
hAlt1 = uicontrol('parent',hPanelMain, 'units','norm', 'pos',[.1,.8,.5,.1], 'style','radio', 'string','Alternative #1');
% Alternative options panel #2
hAlt2 = uipanel('parent',hPanelMain, 'units','norm', 'pos',[.07,.4,.5,.35], 'title','Alternative #2');
hAlt2Title = get(hAlt2, 'TitleHandle');
newPos = get(hAlt2Title,'position') + [0,0,20,0];  % in pixels
set(hAlt2Title, 'style','radio', 'pos',newPos);
hAlt2a = uicontrol('parent',hAlt2, 'units','norm', 'pos',[.2,.6,.7,.3], 'style','checkbox', 'string','Option 1');
hAlt2b = uicontrol('parent',hAlt2, 'units','norm', 'pos',[.2,.2,.7,.3], 'style','checkbox', 'string','Option 2');
% Alternative options panel #3
hAlt3 = uipanel('parent',hPanelMain, 'units','norm', 'pos',[.07,.05,.5,.3], 'title','Alternative #3');
hAlt3Title = get(hAlt3, 'TitleHandle');
newPos = get(hAlt3Title,'position') + [0,0,20,0];  % in pixels
set(hAlt3Title, 'style','radio', 'pos',newPos, 'ForegroundColor','blue');
hAlt3a = uicontrol('parent',hAlt3, 'units','norm', 'pos',[.2,.5,.7,.3], 'style','popup', 'string',{'Option 3a','Option 3b','Option 3c'});

% Main panel and basic alternative control hPanelMain = uipanel('pos',[.1,.1,.8,.8], 'Title','Main', 'units','norm'); hAlt1 = uicontrol('parent',hPanelMain, 'units','norm', 'pos',[.1,.8,.5,.1], 'style','radio', 'string','Alternative #1'); % Alternative options panel #2 hAlt2 = uipanel('parent',hPanelMain, 'units','norm', 'pos',[.07,.4,.5,.35], 'title','Alternative #2'); hAlt2Title = get(hAlt2, 'TitleHandle'); newPos = get(hAlt2Title,'position') + [0,0,20,0]; % in pixels set(hAlt2Title, 'style','radio', 'pos',newPos); hAlt2a = uicontrol('parent',hAlt2, 'units','norm', 'pos',[.2,.6,.7,.3], 'style','checkbox', 'string','Option 1'); hAlt2b = uicontrol('parent',hAlt2, 'units','norm', 'pos',[.2,.2,.7,.3], 'style','checkbox', 'string','Option 2'); % Alternative options panel #3 hAlt3 = uipanel('parent',hPanelMain, 'units','norm', 'pos',[.07,.05,.5,.3], 'title','Alternative #3'); hAlt3Title = get(hAlt3, 'TitleHandle'); newPos = get(hAlt3Title,'position') + [0,0,20,0]; % in pixels set(hAlt3Title, 'style','radio', 'pos',newPos, 'ForegroundColor','blue'); hAlt3a = uicontrol('parent',hAlt3, 'units','norm', 'pos',[.2,.5,.7,.3], 'style','popup', 'string',{'Option 3a','Option 3b','Option 3c'});

Advanced panel-level controls
Advanced panel-level controls

Note that since the hAlt2Title and hAlt3Title radio-buttons are children of their respective uipanel parents, we cannot use a simple uibuttongroup to group them in a mutual-exclusion group. Instead, we must use a dedicated callback function. This is actually quite easy:

% Set the callback for all relevant radio-buttons
hButtonGroup = [hAlt1, hAlt2Title, hAlt3Title];
set(hButtonGroup, 'Callback', {@SelectionCb, hButtonGroup});
% This is the callback function that manages mutual exclusion
function SelectionCb(hSrc,hEvent,hButtonGroup)
   otherButtons = setdiff(hButtonGroup,hSrc);
   set(otherButtons,'value',0);
   set(hSrc,'value',1);  % needed to prevent de-selection
end

% Set the callback for all relevant radio-buttons hButtonGroup = [hAlt1, hAlt2Title, hAlt3Title]; set(hButtonGroup, 'Callback', {@SelectionCb, hButtonGroup}); % This is the callback function that manages mutual exclusion function SelectionCb(hSrc,hEvent,hButtonGroup) otherButtons = setdiff(hButtonGroup,hSrc); set(otherButtons,'value',0); set(hSrc,'value',1); % needed to prevent de-selection end

Related posts:

  1. Rich-contents log panel – Matlab listboxes and editboxes can be used to display rich-contents HTML-formatted strings, which is ideal for log panels. ...
  2. GUI integrated HTML panel – Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....
  3. Images in Matlab uicontrols & labels – Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...
  4. Icon images & text in Matlab uicontrols – HTML can be used to add image icons to Matlab listbox and popup (drop-down) controls. ...
  5. Transparency in uicontrols – Matlab uicontrols' CData property can be customized to provide background transparency....
  6. Transparent uipanels – Matlab uipanels can be made transparent, for very useful effects. ...
GUI Handle graphics Hidden property Pure Matlab TitleHandle uicontrol Undocumented property
Print Print
« Previous
Next »
17 Responses
  1. Alex November 25, 2010 at 02:20 Reply

    In the first image in this article, how did you achieve the horizontal line under the tabs that are not active?

    • Yair Altman November 25, 2010 at 09:20 Reply

      Alex – The uitab control (explained here, with additional articles earlier this month that showed how to customize the tab appearance and behavior), hides the separator line in its internal implementation. In the example on this page I used a Java JTabbedPane directly, and this preserves the separator line. Here’s a simple example:

      [jTabbedPane, hContainer] = javacomponent('javax.swing.JTabbedPane', [20,50,200,200], gcf);
      jTabbedPane.addTab('tab #1',javax.swing.JPanel);
      jTabbedPane.addTab('tab #2',javax.swing.JPanel);

      [jTabbedPane, hContainer] = javacomponent('javax.swing.JTabbedPane', [20,50,200,200], gcf); jTabbedPane.addTab('tab #1',javax.swing.JPanel); jTabbedPane.addTab('tab #2',javax.swing.JPanel);

      • Alex November 26, 2010 at 01:30

        Thanks, that’s exactly what I was looking for. Can you give ma an example how to add a Matlab uicontrol and axes to a tab? Can I set the ‘parent’ property to an uicontrol or is there another way now?

      • Yair Altman November 30, 2010 at 15:26

        Alex – Matlab uicontrols and axes cannot be added to Java containers (well, at least not without a herculean effort).

        This is why MathWorks implemented uitab so that uicontrols/axes are not added directly to the tabs but rather to invisible Matlab containers that are handled by the tab callbacks.

        Under the hood, uitab is a simple JTabbedPane that has no containers, only tabs. So, fixing it such that the missing separator line appears should be a simple enough exercise for you.

        Please post the necessary changes to the uitab.m file (or its relatives) here if and when you succeed.

      • Alex December 1, 2010 at 03:40

        Is it in the schema.m? I tried to modify the line

        jpanel = javax.swing.JPanel(java.awt.BorderLayout);
        [comp2, h.HgContainer] = javacomponent(jpanel,[1 1 1 1], h.parent);

        jpanel = javax.swing.JPanel(java.awt.BorderLayout); [comp2, h.HgContainer] = javacomponent(jpanel,[1 1 1 1], h.parent);

        but I couldn’t see the effect.
        I’m new to Java, so could you please give me more hints?

  2. Alex December 9, 2010 at 06:42 Reply

    I looked at jTabGroup using

    uiinspect(jTabGroup)

    uiinspect(jTabGroup)

    is there a property to make the separator line appear?

  3. Using Java 7 in Matlab R2013a and earlier | Undocumented Matlab June 19, 2013 at 12:11 Reply

    […] Notice that the panel names are still missing. So far no solution for this has been found. Perhaps this can be overcome using the fact that uipanel titles are simply hidden children of the panel object. […]

  4. Ninad kubal February 25, 2014 at 22:44 Reply

    Can i add callback on any property change of matlab uipanel or uicontrol?

    • Yair Altman February 26, 2014 at 00:30 Reply

      @Ninad – you can try to use property-change listeners.

  5. Patrick Holland June 11, 2014 at 06:31 Reply

    Hello Mr. Altman,
    I don’t know where exactly to ask, so I ask here.
    I found in the internet one respond from you

    [jPanel,hPanel] = javacomponent(javax.swing.JPanel);
    set(hPanel, 'units','normalized','position',[.1,.1,.8,.8]);
    hControl = uicontrol('style','pushbutton', 'parent',hPanel, 'string','click me');

    [jPanel,hPanel] = javacomponent(javax.swing.JPanel); set(hPanel, 'units','normalized','position',[.1,.1,.8,.8]); hControl = uicontrol('style','pushbutton', 'parent',hPanel, 'string','click me');

    I have tried this code with Matlab 2013b and the Panel is empty somehow. I don’t know why.
    But my question IS… I want to add an undetermined amount of editboxes and labels to a panel. It can be 1 or 32 or even more.
    I don’t want to resize them according to the space because then they would be extremely too small. So i need a Scrollbar.

    I thought to use a JPanel and put it in a JScrollpane, but i failed at the first step up there. The jPanel is empty.
    Do you know what’s wrong?

    greetings

    • Yair Altman June 11, 2014 at 06:46 Reply

      @Patrick – you used javacomponent incorrectly. Read about the correct usage here: http://undocumentedmatlab.com/blog/javacomponent

      If you’d like my consulting help with setting up a scroll-panel and other such advanced GUI stuff, please contact me offline – my email link is at the top-right of this page.

      • Patrick Holland June 11, 2014 at 06:49

        Hello Mr. Altman,
        i don’t understand. It’s from your own post here.

        https://www.mathworks.com/matlabcentral/newsreader/view_thread/148636

      • Yair Altman June 11, 2014 at 06:54

        @Patrick – just do as I suggested, read the link I provided.

  6. Kusi February 23, 2015 at 04:38 Reply

    In R2014b, it seems like the TitleHandle property is gone. Is there another way to have panel-level check boxes?

    • Yair Altman February 23, 2015 at 05:33 Reply

      @Kusi – no: in HG2 (R2014b) the Matlab uipanel is (finally!) implemented as a true Java Swing component (deriving from JPanel), and the title is implemented as a standard Java titled-border. In other words, it is no longer a separate uicontrol.

      Instead, you can simply place a dedicated checkbox uicontrol on top of the panel, you just need to ensure the position is correct and normalized so that it moves with the panel when it changes its size/position.

    • Garuve May 2, 2017 at 10:52 Reply

      @Yair: Could you provide an example of doing this (in HG2)? I can’t get it to work (i.e. I don’t know how to put a checkbox uicontrol ‘on top of’ the panel?
      Very much appreciated!

      • Matt May 3, 2017 at 02:35

        Garuve, one easy way to put a control on top of a panel simply put the panel as the parent.

        exampleFigure = figure;
        hp = uipanel('Title','Main Panel','FontSize',12,...
                     'BackgroundColor','white',...
                     'Position',[.25 .1 .67 .67],...
                     'Units', 'Normalized');
        cb = uicontrol(hp,'style','checkbox','position',[100,100, 20,20])

        exampleFigure = figure; hp = uipanel('Title','Main Panel','FontSize',12,... 'BackgroundColor','white',... 'Position',[.25 .1 .67 .67],... 'Units', 'Normalized'); cb = uicontrol(hp,'style','checkbox','position',[100,100, 20,20])

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