GUI with moving marker
6 次查看(过去 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 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!