How to remove a plot (if it exists) before replotting? Matlab GUI ButtonDownFcn

Hy, I want to plot a point on mouse-cklick using the ButtonDownFcn. Before it is plotted it should check if there is an existing point. If yes, delete it and plot new point else just plot.
Here my ButtonDownFcn without removing and display of the coordinates in a static text:
function print_coordinates(hObject,eventdata,handles)
C=get(gca,'CurrentPoint');
set(handles.textCoordinates,'Visible','on');
set(handles.textCoordinates,'String',['X: ',num2str(C(1,1)),...
char(10),'Y: ',num2str(C(1,2))]);
hold on
hCoord=plot(C(1,1),C(1,2),'o','Color','r',...
'LineWidth',1.5)
How can i do this?

 採用された回答

Michael Haderlein
Michael Haderlein 2014 年 9 月 17 日

0 投票

I would create such a point right at the start of the program. Use the coordinates (0,NaN) to keep it invisible and save the handle of the plot. Then, in your buttondownfunction, you can simply change the xdata/ydata properties to the values of C.

4 件のコメント

sejo
sejo 2014 年 9 月 17 日
Thx. I now plot the point where i also plot other lines using;
hCoord=plot(0,NaN,'o','Color','r',...
'LineWidth',1.5,'Visible','off');
My ButtonDownFcn is now;
function plot_coordinates
C=get(gca,'CurrentPoint');
set(handles.textCoordinates,'Visible','on');
set(handles.textCoordinates,'String',['X: ',num2str(C(1,1)),...
char(10),'Y: ',num2str(C(1,2))]);
hold on
set(handles.hCoord,'xdata',C(1,1),'ydata',C(1,2),'Visible','on');
But an error (Reference to non-exising field hCoord) occurs. How do i pass the handle of the point?
Adam
Adam 2014 年 9 月 17 日
You need:
handles.hCoord=plot(0,NaN,'o','Color','r',...
'LineWidth',1.5,'Visible','off');
guidata( hObject, handles )
at the point where you plot in order to have hCoord available in your callback.
As Adam said, you need to provide the handle of hCoord. The point where to plot is the OpeningFcn of your figure, such that it reads as
% Choose default command line output for test
handles.output = hObject;
hold(handles.axes1,'all') %NEW
hCoord=plot(0,nan,'ro'); %NEW
handles.hCoord=hCoord; %NEW
% Update handles structure
guidata(hObject, handles);
Do not set the visible property 'off'. It will be invisible as the y-data is NaN. It will be visible when you set it first time to a numeric value. If the property 'visible' is set 'off', it will keep being invisible.
sejo
sejo 2014 年 9 月 17 日
Thx it worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2014 年 9 月 17 日

コメント済み:

2014 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by