GUI with moving marker

조회 수: 10(최근 30일)
Thomas
Thomas 2012년 10월 10일
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
Thomas
Thomas 2012년 10월 11일
Yes, you are right. I want to create a point moving along a graph.
The problem with set(...) is, that the commands (set and line) are executed while the usage of the slider. That´s the reason why the marker will be drawn again and not will be updated. Do you have an idea how I can avoid this problem?

댓글을 달려면 로그인하십시오.

채택된 답변

Dan K
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

추가 답변(3개)

Sachin Ganjare
Sachin Ganjare 2012년 10월 10일
You can try 'cla' function.
Hope it helps!!!
  댓글 수: 1
Thomas
Thomas 2012년 10월 11일
Thank you for your reply!
It works, but not in a way it should.
Because it deletes not only the marker(line-element), it also deletes the drawn graph. This is no huge problem, because the graph can be drawn new each time the slider moves.
The bigger problem is, that the marker related to the second slider is also erased while moving slider number one...

댓글을 달려면 로그인하십시오.


Robert Cumming
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
  댓글 수: 1
Thomas
Thomas 2012년 10월 11일
Thank you for your reply I tried to realize your advice, but I am an absolutely MATLAB novice and so it doesn´t work. Do I have to program the command lines in the callback-function of the slider, like I did?
Please take a look at my code.
% --- Executes on slider movement.
function [hS1]=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
load Data.mat
x=get(hObject,'value');
x_round=round(x)
set(hObject,'value',x_round);
y=(L_KnieAngle3D_orig(x_round,1));
line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r');
h=line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r');
delete(h);

댓글을 달려면 로그인하십시오.


Image Analyst
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
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(......)

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by