GUI with moving marker
7 views (last 30 days)
Show older comments
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.
2 Comments
MRR
on 10 Oct 2012
With marker I understand a point in a plot. If that is the case, what you should do is to change the corresponding coordinate with the set command:
set(h, 'Xdata', xCoordinate,'Ydata', yCoordinate);
PS: h is the marker object
-Mario
Accepted Answer
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
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
0 Comments
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 );
for i=1:length(x)
h = plot ( ax, x(i), y(i), 'rs' );
pause ( 0.1 );
delete ( h );
end
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.
function ClearLinesFromAxes()
axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
return; % from ClearLinesFromAxes
2 Comments
Image Analyst
on 11 Oct 2012
It doesn't do that for me, and I can't see anything in the code that would get rid of the whole graph. For me, it leaves the graph/plot area and just removes the lines. Of course if you plotted a line with plot() it will blow away that also. In that case, you'll have to keep track of which handle is the handle of the curve you plotted and don't delete that one.
handleToKeep = plot(......)
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!