GUI with moving marker
16 views (last 30 days)
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
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:
But it doesn´t work, with this i do not get an marker anymore. I have no idea to solve this problem, please help.
Dan K on 11 Oct 2012
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
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
More Answers (3)
Robert Cumming on 10 Oct 2012
you need to store the handle to the marker, e.g.
d = dialog ( 'windowstyle', 'normal' );
ax = axes ( 'parent', d, 'nextplot', 'add' );
x = [-pi:0.1:pi];
y = sin(x);
plot ( ax, x, y );
h = plot ( ax, x(i), y(i), 'rs' );
pause ( 0.1 );
delete ( h );
Image Analyst on 11 Oct 2012
Edited: Image Analyst on 11 Oct 2012
Call this function right before you draw the "current" line to erase all previous lines on your axes:
% Erases all lines from the current axes.
% The current axes should be set first using the axes()
% command before this function is called,
% as it works from the current axes, gca.
axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
return; % from ClearLinesFromAxes