GUI with moving marker
조회 수: 12 (최근 30일)
이전 댓글 표시
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
MRR
2012년 10월 10일
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
채택된 답변
Dan K
2012년 10월 11일
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
추가 답변 (3개)
Robert Cumming
2012년 10월 10일
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
2012년 10월 11일
편집: Image Analyst
2012년 10월 11일
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
Image Analyst
2012년 10월 11일
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(......)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!