App Designer: Create a Discrete Slider
25 次查看(过去 30 天)
显示 更早的评论
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 个评论
Kojiro Saito
2020-2-5
I think you could create a discrete slider whose values are 0~10. Where do you get stucked?
采纳的回答
Adam Danz
2020-2-5
编辑:Adam Danz
2020-7-1
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 个评论
Adam Danz
2021-6-23
Thanks for the feedback.
Just FYI, AppDesigner already offers discrete knobs (there's a link in the first sentence of my answer).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

