App Designer: Create a Discrete Slider

Background:
Attemtping to develop a survey using MATLAB App Designer. The goal is to ask a user to rate a photo shown from 1-10.
What I'm trying to do:
Create a Slider that would only fix itself to discrete values and store this rating to later on tabulate survey results.
About me:
I'm new to the App Designer of MATLAB, so I'm not really too familiar with the syntax and some keywords, a dumbed down explanation would be much appreciated.
-- Design View --

2 comentarios

Kojiro Saito
Kojiro Saito el 5 de Feb. de 2020
I think you could create a discrete slider whose values are 0~10. Where do you get stucked?
Andres Bergsneider
Andres Bergsneider el 5 de Feb. de 2020
Hi Kojiro, thank you for your prompt response.
The only slider option I see (picutred above) is continiuous. Meaning it will stay placed wherever the user has dropped the arrow key. I'm hoping it would snap to one of the integer values.
-- Screenshot of the Issue --

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 5 de Feb. de 2020
Editada: Adam Danz el 1 de Jul. de 2020
Just be aware that there are discrete knobs (properties) in app designer. But if you want a discrete slider....
Instructions how to make a continuous slider behave as if it were discrete
1) Add the slider to your app in app designer.
2) In the Inspector window, set the Limits, MajorTicks and MajorTickLabels to the discrete values of your choice and remove the MinorTicks since they do not make sense with discrete options.
3) Add a Value Changed callback function by right-clicking the slider > callbacks > Add ValueChangedFcn callback. This will add the new function in Code View.
4) From the Code View, modify the newly added function so that it always snaps to the nearest discrete value on the slider. You can copy-paste the following function except for the first and last line.
function SliderValueChanged(app, event)
value = app.Slider.Value;
% determine which discrete option the current value is closest to.
[~, minIdx] = min(abs(value - event.Source.MajorTicks(:)));
% move the slider to that option
event.Source.Value = event.Source.MajorTicks(minIdx);
% Override the selected value if you plan on using it within this function
value = event.Source.MajorTicks(minIdx);
end

7 comentarios

Andres Bergsneider
Andres Bergsneider el 9 de Feb. de 2020
Adam, what a great breakdown!
Thank you very much for taking your time in helping me out, it totally worked!!
Adam Danz
Adam Danz el 9 de Feb. de 2020
Thanks for the feedback, Andres!
Roche de Guzman
Roche de Guzman el 8 de En. de 2021
In the callback, you can just round the value then replace it back:
value = round(app.Slider.Value); % get then round
app.Slider.Value = value; % set
Adam Danz
Adam Danz el 8 de En. de 2021
Editada: Adam Danz el 8 de En. de 2021
That's true only for consecutive integer ticks.
Consider this set of ticks that are not integers nor at a fixed interval:
[0, 0.25, 0.5, 0.75, 1, 1.5]
or
[0 2 4 6 8 10]
Nitin Phadkule
Nitin Phadkule el 23 de Jun. de 2021
The problem with rounding the value is, the rounded value are rounded for displaying only, buttheir decimal values are still there so rounded values cant be used in if, if else statement for checking equal to value.
Thanks Adam Danz, your answer also helped me to discetise the Knob.
The problem with rounding the value is, the rounded value are rounded for displaying only, buttheir decimal values are still there so rounded values cant be used in if, if else statement for checking equal to value.
Adam Danz
Adam Danz el 23 de Jun. de 2021
Thanks for the feedback.
Just FYI, AppDesigner already offers discrete knobs (there's a link in the first sentence of my answer).
Mohammad Mainul
Mohammad Mainul el 17 de Dic. de 2023
thank you so much Mr. Adam. Your answer helped me a lot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Feb. de 2020

Comentada:

el 17 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by