Slider (legacy) with no room to slide?

3 vues (au cours des 30 derniers jours)
Bruno Luong
Bruno Luong le 26 Août 2023
Commenté : Bruno Luong le 28 Août 2023
I would like to create a slider (legacy version) to select a whole number from 1 to a given n.
For n >= 2 this is OK, the cursor is of size 1/n of the width of the slider, so every step it moves 1 unit coherently,.
figure;
for n = 2:7 % n = 1 ?
pos = [0.1, (n-1)*0.1, 0.8, 0.04];
uicontrol('style','slide','unit', ...
'normalized', 'position', pos,...
'Min', 1, 'Max', n, 'value', 1, 'sliderstep', 1/(n-1)+[0 0], ...
'BackgroundColor', [0 0 0], ...
'Callback', @(h, varargin) disp("v=" + round(h.Value)));
end
Naturally for n=1, I would like cursor to be as large as the slider and it cannot move. Not sure how to set up such slider at the borderline. undocumented Java hack a la Yair is welcome.
As you can try my above code will not make MATLAB very happy for n=1.
  1 commentaire
Bruno Luong
Bruno Luong le 28 Août 2023
For the record I put here some slide to document sliderstep from @the cyclist answer

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 26 Août 2023
I believe this does what you want, for N=1.
I frankly have not fully grokked how this works, but figured I'd post the solution anyway. I also didn't look for the most elegant way to not do this case as a one-off.
figure
for n = 2:7 % n = 1 ?
pos = [0.1, (n-1)*0.1, 0.8, 0.04];
uicontrol('style','slide','unit', ...
'normalized', 'position', pos,...
'Min', 1, 'Max', n, 'value', 1, 'sliderstep', 1/(n-1)+[0 0], ...
'BackgroundColor', [0 0 0], ...
'Callback', @(h, varargin) disp("v=" + round(h.Value)));
end
pos = [0.1, 0, 0.8, 0.04];
uicontrol('style','slide','unit', ...
'normalized', 'position', pos,...
'Min', 1, 'Max', 2, 'value', 1, 'sliderstep', [0 Inf], ...
'BackgroundColor', [0 0 0], ...
'Callback', @(h, varargin) disp("v=" + round(h.Value)));
  1 commentaire
Bruno Luong
Bruno Luong le 26 Août 2023
Oh yeah the trick is to put Inf in sliderstep(2) and something > 1 for Max.
I don't get it either but it's what I look for. Thanks.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by