Hey there everyone. I have a slider which I want to increase like 1:2:10 by dragging the bar with mouse. How can I accomplish that?
Thanks in advance!

1 件のコメント

Rik
Rik 2020 年 5 月 18 日
I don't know if you have already come across this page, but especially since GUIDE is going to be removed from Matlab, here are some useful tips: https://www.mathworks.com/matlabcentral/answers/483657-how-to-create-a-gui

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

 採用された回答

Rik
Rik 2020 年 5 月 17 日
編集済み: Rik 2020 年 5 月 17 日

2 投票

Round the Value property to the nearest value in your array in the callback function.
allowed_vals=1:2:10;%better: store this in the guidata struct
val=3.2;%val=get(hObject,'Value');
[~,idx]=min(abs(allowed_vals-val));
val_rounded=allowed_vals(idx);
%set(hObject,'Value',val_rounded);
disp(val_rounded)

4 件のコメント

John Doe
John Doe 2020 年 5 月 17 日
編集済み: John Doe 2020 年 5 月 17 日
Hello Rik. Thanks for the neat answer. It works.
[~,idx]=min(abs(allowed_vals-val));
val_rounded=allowed_vals(idx);
Could you please explain these two lines? And also by the guidata struct, did you mean where the callback of slider is written and insert the allowable range in there?
Rik
Rik 2020 年 5 月 18 日
Did you read the documentation for these functions?
[~,idx]=min(abs(allowed_vals-val));
%subtract the value of the slider from the allowed values
%then take the absolute value to calculate the distance between the current value and all allowed values
%use the min funtion to find the index with the lowest difference
val_rounded=allowed_vals(idx);
%use the index of the lowest difference to select the value from the array
And the guidata struct is called handles in a GUIDE GUI. It is the variable that is stored with your GUI figure, so it is a good way to share data between callbacks. You can set a value in the StartupFcn and use that in your callback. That way you have a place that makes sense to set an array of allowed values, and you give yourself the option of having some callback modify this array (e.g. if you implement a data set selector that uses different ranges).
John Doe
John Doe 2020 年 5 月 18 日
I see. Thanks for the explanation.
Actually, I am trying to pass this slider value to a filter.
x = imboxfilt(Dummyimage,sliderval)
When I increase the slider, the picture gets blurred. But then when I decrease the value, the blurness does not decrease. I want the blur amount to vary with the slider value.
Is it because the pixels are already averaged and cannot be reverted back? Is there a way to solve this issue? Thanks.
Rik
Rik 2020 年 5 月 18 日
You need to blur the original image, so you will have to store both the original and the processed version.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2020 年 5 月 17 日

コメント済み:

Rik
2020 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by