MATLAB: GUI with moving marker

guimarkermovementslider

Hello
I have programmed an GUI with two sliders. According to the slider movement, a marker should move along a given graph. I have realised this with following code:
% --- 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
x=get(hObject,'value')
x_round=round(x)
set(hObject,'value',x_round)
y=(DataVector(x_round,1))
line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r');
My problem is, that the marker does not vanish while it gets new coordinates from the slider. Thus I get multiple markers along the graph. My first idea was to delete the line-element:
delete(h);
x=get(hObject,'value')
x_round=round(x)
set(hObject,'value',x_round)
y=(DataVektor(x_round,1))
h=line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r');
But it doesn´t work, with this i do not get an marker anymore. I have no idea to solve this problem, please help.

Best Answer

A couple of different options: Perhaps the simplest (although not very speed efficient) is to apply a tag to the marker when you first create it, then use findobj to locate it in the callback.
Do this when you're first creating the figure
hMarker=line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r','Tag','MyMarker');
Then in your callback hMarker_local = findobj(gcf,'Tag','MyMarker'); set(hMarker_local,'Xdata',newX,'YData',newY);
Alternatively you can add extra parameters to your slider callback, one of which is the original hMarker.
A third choice is to nest your callback inside of the function where you're making your plot, in which case the callback has access to variables defined in the parent function, including hMarker.
Hope this helps. Dan