MATLAB: How to move slider-bar with a step-size of two

guiguidematlab guislider

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!

Best Answer

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)