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;
if ~isfield(handles,'hListener')
handles.hListener = ...
addlistener(handles.slider1,'ContinuousValueChange',@respondToContSlideCallback);
end
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) ]);
handles.lastSliderVal = get(handles.slider1,'Value');
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
function respondToContSlideCallback(hObject, eventdata)
handles = guidata(hObject);
newVal = floor(get(hObject,'Value'));
set(hObject,'Value',newVal);
if newVal ~= handles.lastSliderVal
handles.lastSliderVal = newVal;
guidata(hObject,handles);
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!
Best Answer