MATLAB: Slider with non consecutive values

callbacknon consecutiveslider

Hey!
My problem is: I have an object with a range of ID which are not always consecutive.
[handles.trajects{1:20,1}]
ans =
Columns 1 through 9
10816049 10818824 10818825 10818826 10818827 10818828 10818829 10818830 10818831
Columns 10 through 18
10818832 10818833 10819059 10819395 10819396 10819397 10819398 10819399 10819401
Columns 19 through 20
10819402 10819403
I want the user to be able to select the ID through a slider in my GUIDE GUI. This is how its setup:
%set slider
set(handles.slider, 'Max', max([trajects{:,1}]));
set(handles.slider, 'Min', min([trajects{:,1}]));
set(handles.slider, 'Value', min([trajects{:,1}]));
handles.lastSliderValue = min([trajects{:,1}]);
set(handles.slider, 'SliderStep', [1/(max([trajects{:,1}])-min([trajects{:,1}])) 1/20]);
guidata(hObject,handles);
In this case the Min/Max are 10816049 and 12262539. I want the slider to go to the NEXT/PREVIOUS ID if the user clicks the arrows (SliderStep 1, diff=1) and to the NEAREST ID when the user slides to some other position. Here is the code to achieve this:
function slider_Callback(hObject, eventdata, handles)
disp(num2str(get(handles.slider,'Value')));
sliderValue = get(hObject, 'Value');
sliderValue = round(sliderValue);
trajects = handles.trajects;
%check if last value was 1 away
diff = sliderValue - handles.lastSliderValue;
if abs(diff) == 1
%go to next flightID
traj_idx = find([trajects{:,1}] == handles.lastSliderValue) + diff;
else
%calculate the nearest flightID
[~, traj_idx] = min(abs([trajects{:,1}] - sliderValue));
end
set(hObject, 'Value', trajects{traj_idx,1});
%make the selected ID in the plot...
%...
handles.lastSliderValue = trajects{traj_idx,1};
guidata(hObject,handles);
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp(num2str(get(handles.slider,'Value')));
The problem is: Lets say I am on 10818824 (handles.lastSliderValue is on 10818824). Clicking the arrow left displays 10817966 and clicking right displays 10817968. It seems the slider is stuck on 10817967 internally. Checking by clicking pushbutton7 displays the correct value 10818824. I cannot seem to find the problem 🙁

Best Answer

If you have your slider values in an array then just map onto them and use the id (which is consecutive) into the array as the value in the slider. Then you can just have a min of 1, a max of 20 and index into your array of true values based on the slider value.