MATLAB: My slider disappears and it comes up with that error

guiimage processingmousescrollwheelfcnslidersuicontrol

I am trying to write a function for having the slider moves according to the mouse wheel. Here is my code
function mouseScroll(~,eventdata,I)
handles = guidata(gcf);
S = round((get(handles.SliderFrame,'Value')));
sno = size(MyMatrix);
UPDN = eventdata.VerticalScrollCount;
S = S - UPDN;
if (S < 1)
S = 1;
elseif (S > sno)
S = sno;
end
if sno > 1
set(handles.SliderFrame,'Value',S);
set(handles.Edit1, 'String', sprintf('Slice# %d / %d',S, sno));
else
set(handles.Edit1, 'String', '2D image');
end
frameindex = max(1, min(S, NumFrames));
handles.frameindex = frameindex+1;
ff = filelist{frameindex};
I=dicomread(ff);
imshow(I, 'parent', handles.axes1);
guidata(hFig,handles);
drawnow()
end
I get the error "Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range Control will not be rendered until all of its parameter values are valid" when the slider exceeds the maximum value, where it disappears.
Any idea?

Best Answer

Replace
set(handles.SliderFrame,'Value',S);
with
if S>get(handles.SliderFrame,'Max')
S=get(handles.SliderFrame,'Max');
elseif S<get(handles.SliderFrame,'Min')
S=get(handles.SliderFrame,'Min');
end
set(handles.SliderFrame,'Value',S);