GUI with moving marker

6 次查看(过去 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 个评论
MRR
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
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 个评论
Thomas
Thomas 2012-10-11
Thank you for your code.
But I have two problems with it.
The first is, that it deletes also the graph which is displayed in the figure. But this can be solved by drawing the graph an the line object after erasing the lines.
The second is, that I have two sliders and when I use the second one, the marker of the first slider will be deleted.
Here the code which shows how i did use your function.
% --- 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));
ClearLinesFromAxes();
line(x_round,y,'linestyle','none','marker','o','markeredgecolor','r');
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(......)

请先登录,再进行评论。

类别

Help CenterFile 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!

Translated by