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

Sliders in Matlab GUI – part 2

July 5, 2018 9 Comments

Exactly 3 years ago I posted about various alternatives for embedding sliders in Matlab GUI. Today I will follow up on that post with a description of yet another undocumented builtin alternative – controllib.widget.Slider. A summary of the various alternatives can be seen in the following screenshot:

Slider alternatives in Matlab GUI
Slider alternatives in Matlab GUI

The controllib.widget.Slider component is a class in Matlab’s internal controllib package (last week I discussed a different utility function in this package, controllib.internal.util.hString2Char).

controllib.widget.Slider accepts 3 input arguments: containing figure handle, position in pixels, and data values. For example:

>> hSlider = controllib.widget.Slider(gcf, [10,10,100,50], 5:25)
hSlider =
  Slider with properties:
        Data: [6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25]
       Index: 11
       Value: 15
    FontSize: 8
    Position: [10 10 100 50]

>> hSlider = controllib.widget.Slider(gcf, [10,10,100,50], 5:25) hSlider = Slider with properties: Data: [6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25] Index: 11 Value: 15 FontSize: 8 Position: [10 10 100 50]

This creates an invisible axes at the specified figure location and displays graphic axes objects that provide the appearance of the slider. When you move the slider’s knob, or click its centerline or arrows (“Steppers”), the slider’s value changes accordingly.
You can attach a callback function to the slider as follows:

myCallback = @(h,e) disp(h.Value);  % as an example
addlistener(hSlider, 'ValueChanged', myCallback);

myCallback = @(h,e) disp(h.Value); % as an example addlistener(hSlider, 'ValueChanged', myCallback);

Note that controllib.widget.Slider is based on pure-Matlab code and fully-supported functionality. The Matlab source code is available (%matlabroot%/toolbox/shared/controllib/graphics/+controllib/+widget/Slider.m) and quite readable. So while it does not actually work with the new web-based uifigures, is should be relatively easy to adapt the code so that this component could be displayed in such uifigures.
Below is a script to recreate the screenshot above. Note the two alternative mechanisms for setting properties (Java setter-method notation, and HG set notation):

hFig = figure('Color','w');
 
% 1. controllib.widget.Slider
hSlider1 = controllib.widget.Slider(hFig, [10,10,100,50], 1:20);
hSlider1.Value = 12;
 
% 2. uicontrol
hSlider2 = uicontrol('style','slider', 'units','pixels', 'pos',[10,80,100,20], 'Min',0', 'Max',20, 'Value',12);
 
% 3. JScrollBar
jSlider3 = javaObjectEDT(javax.swing.JScrollBar);
jSlider3.setOrientation(jSlider3.HORIZONTAL);  % Java setter-method notation
set(jSlider3, 'VisibleAmount',1, 'Minimum',0, 'Maximum',20, 'Value',12);  % HG set notation
[hSlider3, hContainer3] = javacomponent(jSlider3, [10,130,100,20], hFig);
 
% 4. JSlider #1
jSlider4 = javaObjectEDT(javax.swing.JSlider(0,20,12))
jSlider4.setBackground(java.awt.Color.white);  % Java setter-method notation
set(jSlider4, 'MinorTickSpacing',1, 'MajorTickSpacing',4, 'SnapToTicks',true, 'PaintLabels',true);  % HG set notation
[hSlider4, hContainer4] = javacomponent(jSlider4, [10,180,100,30], hFig);
 
% 5. JSlider #2
jSlider5 = javaObjectEDT(javax.swing.JSlider(0,20,12))
jSlider5.setBackground(java.awt.Color.white);  % Java setter-method notation
jSlider5.setPaintTicks(true);
set(jSlider5, 'MinorTickSpacing',1, 'MajorTickSpacing',4, 'SnapToTicks',true, 'PaintLabels',true);  % HG set notation
[hSlider5, hContainer5] = javacomponent(jSlider5, [10,230,100,40], hFig);

hFig = figure('Color','w'); % 1. controllib.widget.Slider hSlider1 = controllib.widget.Slider(hFig, [10,10,100,50], 1:20); hSlider1.Value = 12; % 2. uicontrol hSlider2 = uicontrol('style','slider', 'units','pixels', 'pos',[10,80,100,20], 'Min',0', 'Max',20, 'Value',12); % 3. JScrollBar jSlider3 = javaObjectEDT(javax.swing.JScrollBar); jSlider3.setOrientation(jSlider3.HORIZONTAL); % Java setter-method notation set(jSlider3, 'VisibleAmount',1, 'Minimum',0, 'Maximum',20, 'Value',12); % HG set notation [hSlider3, hContainer3] = javacomponent(jSlider3, [10,130,100,20], hFig); % 4. JSlider #1 jSlider4 = javaObjectEDT(javax.swing.JSlider(0,20,12)) jSlider4.setBackground(java.awt.Color.white); % Java setter-method notation set(jSlider4, 'MinorTickSpacing',1, 'MajorTickSpacing',4, 'SnapToTicks',true, 'PaintLabels',true); % HG set notation [hSlider4, hContainer4] = javacomponent(jSlider4, [10,180,100,30], hFig); % 5. JSlider #2 jSlider5 = javaObjectEDT(javax.swing.JSlider(0,20,12)) jSlider5.setBackground(java.awt.Color.white); % Java setter-method notation jSlider5.setPaintTicks(true); set(jSlider5, 'MinorTickSpacing',1, 'MajorTickSpacing',4, 'SnapToTicks',true, 'PaintLabels',true); % HG set notation [hSlider5, hContainer5] = javacomponent(jSlider5, [10,230,100,40], hFig);

For additional details regarding the other slider alternatives, please refer to my earlier post on this subject.
Have you ever used another interesting utility or class in Matlab’s builtin packages? If so, please tell us about it in a comment below.

Related posts:

  1. Sliders in Matlab GUI – Professional-looking slider controls can easily be integrated in Matlab GUI. ...
  2. uiundo – Matlab's undocumented undo/redo manager – The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....
  3. Matlab and the Event Dispatch Thread (EDT) – The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....
  4. Blurred Matlab figure window – Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...
  5. Customizing contour plots part 2 – The contour lines of 3D Matlab plot can be customized in many different ways. This is the 2nd article on this issue. ...
  6. Matlab toolstrip – part 5 (icons) – Icons can be specified in various ways for toolstrip controls and the app window itself. ...
GUI Internal component Pure Matlab
Print Print
« Previous
Next »
9 Responses
  1. Zoltan Csati July 6, 2018 at 14:18 Reply

    In R2015b I only see the *+app*, the and the *+plot* packages inside the *+controllib* package, no *widget*.

    • Yair Altman July 6, 2018 at 14:29 Reply

      @Zoltan – R2015b is 6 releases older than the latest Matlab release, so surely this is no surprise. New functions are being added to Matlab with each new release. If you upgrade to one of the latest Matlab releases you will be able to use this function.

  2. medi July 20, 2018 at 00:19 Reply

    how can i plot many data with slider in Gui?

    • Yair Altman July 20, 2018 at 00:33 Reply

      @Medi – I am not sure what you mean. I discussed a slider that has several knobs (“range slider”) here: https://undocumentedmatlab.com/blog/sliders-in-matlab-gui#range

  3. sgs January 6, 2019 at 15:55 Reply

    do any of these sliders execute a callback not just when the values is changed, or when the mouse button is released, but as the mouse *moves*?

    I’m trying to implement a slider where the limits automatically expand as you get to the edges. I don’t think this is possible with uicontrol, and i wonder if it’s possible with the others.

    • Yair Altman January 6, 2019 at 16:02 Reply

      You can do this with practically all of them. See here for example: https://undocumentedmatlab.com/blog/continuous-slider-callback

  4. Don February 5, 2020 at 09:03 Reply

    I do not see a visible (on/off) property. Is there a way hide/unhide these, specifically controllib.widget.Slider.
    I currently just use Position and move it out of visible range. Also, can it be deleted? (It can, but leaves the widget).

    • Yair Altman February 5, 2020 at 10:52 Reply

      @Don – you can control the components of controllib.widget.Slider using its internal properties, as follows:

      >> warning off MATLAB:structOnObject  % avoid warning
      >> hSlider = controllib.widget.Slider(hFigure, [10,10,100,50], 1:20);
      >> sSlider = struct(hSlider)
      s = 
        struct with fields:
               Data: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
              Index: 10
              Value: 10
           FontSize: 8
           Position: [10 10 100 50]
              Data_: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
             Index_: 10
             Scale_: 'linear'
               Axes: [1×1 Axes]
              Lines: [2×1 Line]
                Dot: [1×1 Patch]
           Steppers: [2×1 Patch]
               Text: [1×1 Text]
              Cache: []
          Listeners: [1×1 event.listener]
             RADIUS: 0.25
             OFFSET: 0.75
              THETA: [1×41 double]
                 XC: [1×41 double]
                 YC: [1×41 double]
      >> set(allchild(sSlider.Axes),'Visible','off');  % or 'on' to make visible again
      >> warning on MATLAB:structOnObject  % restore warning

      >> warning off MATLAB:structOnObject % avoid warning >> hSlider = controllib.widget.Slider(hFigure, [10,10,100,50], 1:20); >> sSlider = struct(hSlider) s = struct with fields: Data: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] Index: 10 Value: 10 FontSize: 8 Position: [10 10 100 50] Data_: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] Index_: 10 Scale_: 'linear' Axes: [1×1 Axes] Lines: [2×1 Line] Dot: [1×1 Patch] Steppers: [2×1 Patch] Text: [1×1 Text] Cache: [] Listeners: [1×1 event.listener] RADIUS: 0.25 OFFSET: 0.75 THETA: [1×41 double] XC: [1×41 double] YC: [1×41 double] >> set(allchild(sSlider.Axes),'Visible','off'); % or 'on' to make visible again >> warning on MATLAB:structOnObject % restore warning

      You can also control individual slider components this way. For example, to hide the stepper triangles on the right and on the left:

      set(sSlider.Steppers,'Visible','off')

      set(sSlider.Steppers,'Visible','off')

      To delete the slider altogether, delete its container axes:

      delete(sSlider.Axes)

      delete(sSlider.Axes)

  5. matse April 7, 2021 at 18:32 Reply

    Is there a possibility to rotate the controllib.widget.Slider? So to have it vertical?

    Thanks!

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 (email)
  •  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
ActiveX (6) 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) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Iñigo (2 days 10 hours ago): Thanks Yair. I didn’t realize it was posted just above. It would be great to know about a solution soon
  • Yair Altman (3 days 17 hours ago): @Iñigo – your query is related to Seth’s question above. We can access the currently-browsed URL in JavaScript (for example: jBrowserPanel.executeScript...
  • Iñigo (5 days 9 hours ago): I am looking for setting a UI that allows following process: opening a web site, navigate inside this web site and at some specific moments (i.e. when the figure is closed or a button...
  • Nicholas (12 days 14 hours ago): Yair, this works wonderfully! I can’t thank you enough!
  • Collin (14 days 18 hours ago): Seth Good point, I am using 2022b, mathworks seems to have started using CEF browsers from 2019a, best I can tell. take a look at the package com.mathworks.toolbox.matla...
  • Seth (15 days 10 hours ago): Collin, What version of MATLAB are you using?
  • Collin (20 days 16 hours ago): Seth, I have had some success executing javascript that requires no return value by executing it directly (sort of) on the org.cef.browser.CefBrowser that a...
  • Coo Coo (22 days 11 hours ago): FFT-based convolution is circular whereas MATLAB’s conv functions have several options (‘valid’, ‘same’, ‘full’) but unfortunately not...
  • Seth (22 days 14 hours ago): No luck with removing the space.
  • Seth (22 days 16 hours ago): The javascript code works fine running the application from the 2019b desktop version and the 2016b deployed version.
  • Seth (22 days 16 hours ago): I have been using this browser functionality in 2016b because it works fully in deployed applications in that version. However, because of Java 7 being flagged as a security risk, I...
  • Yair Altman (22 days 16 hours ago): I’ve never tested javascript callbacks, but perhaps you should try removing the extra space after the “matlab:” protocol specifier. Does it make any difference?
  • Seth (22 days 17 hours ago): I have been using this functionality in 2016b since it works in deployed applications and have not had a reason to need to upgrade but with java 7 being flagged as a security risk I am...
  • KEVIN (42 days 17 hours ago): I apologize, I intended my response to fall under ‘T’ but did not seem to work. I was referring to the bit of code posted by ‘T’ regarding the toolgroup and...
  • Yair Altman (42 days 18 hours ago): It is not clear from your comment which code exactly you are referring to and what you see differently in 19b vs. 20b, so I cannot assist with your specific query. In general I...
Contact us
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