MATLAB Answers

0

How can I force a slider UIcontrol object in GUIDE to move in discrete steps?

I would like to create a slider UIcontrol object that moves only to integer values between a range of 1 and 16. By default, the slider can be dragged to non-integer values. How can I force the slider to move to only integer values?

Products


Release

R2015a

2 Answers

Answer by MathWorks Support Team on 5 Sep 2018
Edited by MathWorks Support Team on 5 Sep 2018
 Accepted Answer

In order to create a slider UIcontrol object that moves to only integer values, we would need to ensure that 1) clicking the arrow keys and 2) dragging the slider, both move the slider to an integer position. The following illustrate how to do this:
1) Clicking the arrow keys
In order to ensure that clicking the arrow keys moves slider one unit, we can set the minimum step size of the slider to be 1/(MaxSliderValue-MinSliderValue). In this particular example, you would set it as 1/15 units. This can be set by using the 'SliderStep' property of the slider object. In the example code, we have set the 'SliderStep' to [1/15 1]. This means that clicking on the left or right arrow would move the slider 1/15 normalized units (or 1 integer value) and clicking the space between the slider and the arrows would move it completely to one side (this is set by the second element of 'SliderStep', which is equal to 1 in this example).
2) Dragging the slider
In order to ensure that dragging the slider moves it to an integer position, we can round off the value pointed by the slider in the callback function, and also set the position of the slider to this rounded off value.
The following example code illustrates this:
function myslider
figure;
sld = uicontrol('Style', 'slider',...
'Min',1,'Max',16,'Value',8,...
'Units', 'Normalized',...
'Position', [0.3 0.48 0.4 0.04],...
'Callback', @print_val,...
'SliderStep', [1/15 1]);
end
function print_val(hObject,callbackdata)
newval = hObject.Value; %get value from the slider
newval = round(newval); %round off this value
set(hObject, 'Value', newval); %set slider position to rounded off value
disp(['Slider moved to ' num2str(newval)]); %display the value pointed by slider
end
This screenshot shows the generated GUI and the output at the command line after playing around with the slider for a while:

  0 Comments

Sign in to comment.


Answer by Mohsina Zafar on 12 May 2017
Edited by Mohsina Zafar on 12 May 2017

Hello,
I need to use this 'newval' in my rest of the code i.e. as newval changes by sliding the slider, a specific variable value in the main code changes accordingly.
How can I do this?

  3 Comments

The callback code posted does a
set(hObject, 'Value', newval);
so once the callback has run, you can just get() the Value property of the slider to get the rounded value.
Hello Walter,
I have in my main code a line:
threshRGB = multithresh(image,newval);
This is not in the callback function. But I want this newval to be updated as the slider moves so that my image can be thresholded into newval parts.
threshRGB = multithresh(image, get(handles.TheSlider, 'Value') );

Sign in to comment.