Discretizing matlab slider GUI
Show older comments
Hi all. I'm wondering how to make a slider within Matlab GUI whose values are discretized.
So for example, I want to create a simple slider that cycles through integers 1 - 13.
I have tried adjusting the control properties Max / Min and sliderStep. These work fine for discretizing the slider arrows, but dragging the slider "square" itself and moving it to the left and right is still rather "continuous". I want to only be able to see or select integers within the range. Is this possible?
Thanks, Hamad
Accepted Answer
More Answers (1)
M. Massing
on 11 Aug 2015
In case someone else has the same problem in the future, here is a much easier way I found using the slider's callback function:
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val=round(hObject.Value);
hObject.Value=val;
This way you round the non-integer value of the slider to the closes integer and then let the thumb indicator snap to that integer value.
Also sorry for gravedigging
3 Comments
Andrew C.
on 3 May 2016
Make sure you also set SliderStep appropriately, or else the step may be too small to escape the rounding behaviour, and users trying to use the arrow keys may get frustrated. Great refinement of Geoff's answer above!
Kelsey Pettrone
on 10 Nov 2020
I cant add a callback that says (H0bject,eventdata,handles). How do i do this with just
function SlideryearValueChanged(app, event). this is the only option i can find for a call back. Please help.
Geoff Hayes
on 11 Nov 2020
Kelsey - perhaps you can do something like
function SliderValueChanged(app, event)
roundedValue = round(app.Slider.Value);
app.Slider.Value = roundedValue;
end
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!