Discretizing matlab slider GUI

102 ビュー (過去 30 日間)
Hamad
Hamad 2014 年 9 月 3 日
コメント済み: Geoff Hayes 2020 年 11 月 11 日
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

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 3 日
Hamad - you will have to add some code in order to discretize the slider for the case where you slide the "slider" without using the arrows. In the yourGuiName_OpeningFcn of a GUI named yourGuiName, I added the following code
function yourGuiName_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% add a continuous value change listener
if ~isfield(handles,'hListener')
handles.hListener = ...
addlistener(handles.slider1,'ContinuousValueChange',@respondToContSlideCallback);
end
% set the slider range and step size
numSteps = 13;
set(handles.slider1, 'Min', 1);
set(handles.slider1, 'Max', numSteps);
set(handles.slider1, 'Value', 1);
set(handles.slider1, 'SliderStep', [1/(numSteps-1) , 1/(numSteps-1) ]);
% save the current/last slider value
handles.lastSliderVal = get(handles.slider1,'Value');
% Update handles structure
guidata(hObject, handles);
I'm not sure if you are using the slider Callback or have created something similar to the above which is more flexible. So all we've done is to create the listener, initialized the slider, and updated the handles structure.
Now we have to define the callback, respondToContSlideCallback as
% --- Executes on slider movement.
function respondToContSlideCallback(hObject, eventdata)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% first we need the handles structure which we can get from hObject
handles = guidata(hObject);
% get the slider value and convert it to the nearest integer that is less
% than this value
newVal = floor(get(hObject,'Value'));
% set the slider value to this integer which will be in the set {1,2,3,...,12,13}
set(hObject,'Value',newVal);
% now only do something in response to the slider movement if the
% new value is different from the last slider value
if newVal ~= handles.lastSliderVal
% it is different, so we have moved up or down from the previous integer
% save the new value
handles.lastSliderVal = newVal;
guidata(hObject,handles);
% display the current value of the slider
disp(['at slider value ' num2str(get(hObject,'Value'))]);
end
Now when you run the code, you will only see the integer values being written to the Command Window whenever you use the slider arrow buttons or manually move the slider. The difference between each integer will always be one. So we ignore any "slides" in between any two consecutive integers.
Try the above and see what happens!
  2 件のコメント
Hamad
Hamad 2014 年 9 月 4 日
Thank you very much, Geoff!
Alexei
Alexei 2014 年 11 月 11 日
Ugh. I also wanted to make my slider be integer only... I give up. This is too much work. I think I'll just make a user-input text box and make the program error() if a user puts in anything other than allowed integers.

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

その他の回答 (1 件)

M. Massing
M. Massing 2015 年 8 月 11 日
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 件のコメント
Kelsey Pettrone
Kelsey Pettrone 2020 年 11 月 10 日
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
Geoff Hayes 2020 年 11 月 11 日
Kelsey - perhaps you can do something like
function SliderValueChanged(app, event)
roundedValue = round(app.Slider.Value);
app.Slider.Value = roundedValue;
end

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by