- Undocumented Matlab - https://undocumentedmatlab.com -

Sliders in Matlab GUI – part 2

Posted By Yair Altman On July 5, 2018 | 9 Comments

Exactly 3 years ago I posted about various alternatives for embedding sliders in Matlab GUI [1]. 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 [2]).

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]

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

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

For additional details regarding the other slider alternatives, please refer to my earlier post [1] 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.

Categories: GUI, Medium risk of breaking in future versions, Semi-documented function


9 Comments (Open | Close)

9 Comments To "Sliders in Matlab GUI – part 2"

#1 Comment By Zoltan Csati On July 6, 2018 @ 14:18

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

#2 Comment By Yair Altman On July 6, 2018 @ 14:29

@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.

#3 Comment By medi On July 20, 2018 @ 00:19

how can i plot many data with slider in Gui?

#4 Comment By Yair Altman On July 20, 2018 @ 00:33

@Medi – I am not sure what you mean. I discussed a slider that has several knobs (“range slider”) here: [9]

#5 Comment By sgs On January 6, 2019 @ 15:55

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.

#6 Comment By Yair Altman On January 6, 2019 @ 16:02

You can do this with practically all of them. See here for example: [10]

#7 Comment By Don On February 5, 2020 @ 09:03

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

#8 Comment By Yair Altman On February 5, 2020 @ 10:52

@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

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

To delete the slider altogether, delete its container axes:

delete(sSlider.Axes)

#9 Comment By matse On April 7, 2021 @ 18:32

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

Thanks!


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/sliders-in-matlab-gui-part-2

URLs in this post:

[1] alternatives for embedding sliders in Matlab GUI: http://undocumentedmatlab.com/blog/sliders-in-matlab-gui

[2] controllib.internal.util.hString2Char: http://undocumentedmatlab.com/blog/string-char-compatibility

[3] Sliders in Matlab GUI : https://undocumentedmatlab.com/articles/sliders-in-matlab-gui

[4] uiundo – Matlab's undocumented undo/redo manager : https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager

[5] Matlab and the Event Dispatch Thread (EDT) : https://undocumentedmatlab.com/articles/matlab-and-the-event-dispatch-thread-edt

[6] Blurred Matlab figure window : https://undocumentedmatlab.com/articles/blurred-matlab-figure-window

[7] Customizing contour plots part 2 : https://undocumentedmatlab.com/articles/customizing-contour-plots-part-2

[8] Matlab toolstrip – part 5 (icons) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons

[9] : https://undocumentedmatlab.com/blog/sliders-in-matlab-gui#range

[10] : https://undocumentedmatlab.com/blog/continuous-slider-callback

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.