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 (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
  • Sunham (2 days 19 hours ago): This is an old article, but the issue persists even in 2023. 2023a: z = mat2cell(1:1e6,1,repmat(1,1,1e 6)); f = @() cellfun(‘isempty’, z); g = @() cellfun(@isempty,z);...
  • Yair Altman (12 days 9 hours ago): Robot only runs when you tell it to run a command such as keyPress. If you don’t tell it to run a command, it uses no CPU, so there’s no need to remove the Robot...
  • Eric (12 days 20 hours ago): Hey @Kevin, can you share your code about create group of figures in the AppContainer? The container of multiples uifigures could be an amazing improvement over AppDesigner and its...
  • Elsa Smith (13 days 11 hours ago): I recently used java.awt.Robot to perform GUI testing on MATLAB and found it to be an extremely easy and useful way to control mouse movements.
  • Elsa Smith (13 days 11 hours ago): I’m suspecting that the slow performance of my GUI may be due to the use of java.awt.Robot. Is there a way to cancel/stop/remove the robot after it has been created, or is...
  • Michelle Kline (14 days 4 hours ago): *edit* tip about fopen(), not about fwrite(). ‘Wb’ vs. ‘wb’
  • Michelle Kline (14 days 4 hours ago): Thank you, Yair! With this previously-unknown-to-me tip about fwrite() performance, you have saved me literally hours of processing time. Michelle Kline Department of...
  • Alessandro Beda (26 days 16 hours ago): I found what I think is a bug related to this (tested in R2022 and R2023a). If I add a “ButtonDownFcn” to the plots (see example below), then the modified...
  • Nicholas (28 days 7 hours ago): Yair, Changing the desktop help options did not solve the issue. Though, it’s unclear how I could change these options in the Runtime, if that’s what you meant? I should...
  • Yair Altman (32 days 2 hours ago): @Francisco – this is one of those cases where you should ask MathWorks support. After all, you’re trying to use a supported Matlab functionality when you encountered...
  • Francisco Campos (32 days 14 hours ago): Hello, thanks for all your work that has been immensely useful for those working in the Matlab environment. I have been trying to replace matlabcontrol with the official...
  • Yair Altman (36 days 14 hours ago): Kei – this is possible, I believe that I saw this ability somewhere, a few years ago. I don’t remember exactly where, it will require a bit of research, but...
  • Kei (36 days 17 hours ago): Hello Yair Thank you for this great article. I would like to freeze first two columns in uitable. Do you know if such option is available? Since looks like this option is not available...
  • Andrés Aguilar (40 days 4 hours ago): Hello, has anyone tried to change the language of the DateComboBox? For example English -> French ————&# 8212;—- January -> Janvier April...
  • Yair Altman (49 days 2 hours ago): I posted my treeTable utility 10 years ago for anyone to use freely, on an as-is basis, without any warranty, updates or support. If you need any customization or assistance...
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