Marcel - you almost have the correct code, you just need to pass a vector as the value for the Xlim parameter. Since slider1 corresponds to the minimum value for you axes limit, then just change the above code to
function slider1_Callback(hObject, eventdata, handles)
minVal = get(handles.slider1,'Value');
maxVal = get(handles.slider2,'Value');
set(handles.axes1,'Xlim',[minVal maxVal],'XTick',floor(linspace(minVal,maxVal,5)));
I've added the XTick parameter so that the x-axis ticks are linearly spaced from the minimum to the maximum value along the axis (there are 5 ticks displayed).
And this same code can be re-used for the slider2_Callback. In order to prevent code duplication, you can do something like the following
function slider1_Callback(hObject, eventdata, handles)
adjustAxisLimits(handles);
function slider2_Callback(hObject, eventdata, handles)
adjustAxisLimits(handles);
function adjustAxisLimits(handles)
minVal = get(handles.slider1,'Value');
maxVal = get(handles.slider2,'Value');
set(handles.axes1,'Xlim',[minVal maxVal],'XTick',floor(linspace(minVal,maxVal,5)));
So both slider callbacks call the same function to adjust the limits.
Best Answer