Slider component, unity steps

5 views (last 30 days)
Jason
Jason on 3 Mar 2015
Edited: Stephen23 on 5 Mar 2015
I have a sldier component that I want to increment in unity steps. The minimum needs to be 1 and the max is a variable but will be between 10 and 30.
I can't understand the set properties of the slider component, it appears min and max work but min must be 0, it can't be 1. But then the step size seems to be in fractions??

Accepted Answer

Adam
Adam on 3 Mar 2015
Edited: Adam on 3 Mar 2015
min and max can both be anything numeric provided min is less than max. I would advise setting both in the same statement (and 'value' too) otherwise you can run into problems if you set the min first and it is above or equal to the current max or value property of the slider.
step size is in normalised units based on the range max - min of your slider.
figure; uicontrol( 'Style', 'slider', 'Min', 1, 'Max', 10, 'Value', 1 );
works, for example. Setting slider step is a little more complicated though. I wrote a custom class that handles that so it is a while since I have had to do it manually to get nicely rounded increments.
  3 Comments
Stephen23
Stephen23 on 4 Mar 2015
Edited: Stephen23 on 4 Mar 2015
This does not actually answer the original request "I want to increment in unity steps", and implies that it is difficult to do so!
The statement "Setting slider step is ... more complicated though" is completely unjustified: see my answer for the (easy!) solution to create unity slider steps.
Adam
Adam on 4 Mar 2015
Edited: Adam on 4 Mar 2015
The statement may be unjustified when the 'a little' is missed out of the quote! I figured if he wanted more details on that there would be a follow-up question though.
Enforcing unity values on a slider takes more effort than just setting the steps. It wasn't clear from the question whether Jason wished to enforce only integer values on the slider or not.
If so then just formalising what I already wrote that step size is normalised based on the range max - min into the trivial maths equivalent is not sufficient. As soon as you drag the slider you then get values outwith those defined by the steps which may or may not be a problem.
For my usage I ensure that values outside of the step range cannot be set on the slider but such a solution is over the top if it isn't a requirement!

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 4 Mar 2015
Edited: Stephen23 on 5 Mar 2015
It is easy to define the slider steps to be unity (or any other value): you just need to divide the desired step size by the range of the slider. Here is some demonstration code:
val_min = -3;
val_max = 14;
stepSz = [1,5]; % <- [minorStep,majorStep]
uicontrol('Style','slider', 'Units','normalized',...
'Position',[0.1,0.1,0.8,0.1], 'Min',val_min, 'Max',val_max',...
'SliderStep',stepSz/(val_max-val_min),...
'Callback',@(h,~)disp(get(h,'Value')));
Note how the slider step is simply defined as stepSz/(val_max-val_min), i.e. the desired step size divided by the range. Note the step size has two values: [minorStep,majorStep], the documentation explains these as:
  • The slider Value property increases or decreases by the value of minorstep when the user presses an arrow key.
  • The slider Value property increases or decreases by the value of majorstep when the user clicks the slider trough.
Note that sliding the bar with the mouse will still result in non-integer values, and some floating-point-precision-rounding may result in non-integer values too. This can be resolved by simply rounding the Value whenever it is used, outside of the slider environment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!