Is there a way to restrict the values a slider in an app can take? I want to define an array of increasing numbers and have that be all the values the slider is allowed to take as I move it and NOT take on any other values. So for example if the first two values in my array are 1.23 and 2.45, I want the slider two start at 1.23 and when I move it to the right, it should go to 2.45 and NOT 1.4 or anything like that.
Thanks in advance!

 採用された回答

Cris LaPierre
Cris LaPierre 2022 年 12 月 12 日
編集済み: Cris LaPierre 2022 年 12 月 12 日

0 投票

Is the spacing between values uniform? If so, you can set the step property.
If not, I think you would have to have your callback function process the slider value and set it to the predetermined value that is closest.
% Assume this is the slider value
value = 2.1;
% Assume this is the list of values you want the slider to take
myVals = [1.23 2.45 3.76 4.01];
% find the closest value
[~,ind] = min(abs(myVals-value))
ind = 2
valAct = myVals(ind)
valAct = 2.4500
Once you have identified the predefind value to use, set the slider's value property to that value.
app.Slder.Value = valAct;
I wrote the code the way I did so that it will execute here. You will of course need to adapt it to work within your app.

3 件のコメント

Harpreet
Harpreet 2022 年 12 月 12 日
That worked, thank you for your help
Harpreet
Harpreet 2022 年 12 月 13 日
Would you know if there is a way to set the step size of it using uislider instead of uicontrol, given the spacing is uniform?
Cris LaPierre
Cris LaPierre 2022 年 12 月 14 日
Sorry for the confusion. Only sliders in live tasks have a step property. So a uislider has the same properties as the slider component in an app.
If you want to only display accepted values on the tick labels, I would probably just programmatically set the slider ticks in a startupFcn. Note that a slider does not snap to the ticks. The previous answer I shared could be used for that purpose.
% Code that executes after component creation
function startupFcn(app)
app.Slider.MajorTicks = 1.23:1.22:15;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

質問済み:

2022 年 12 月 12 日

コメント済み:

2022 年 12 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by