Clear a line drawn in plot.

Hello I would like to clear a line that I draw in my graph but I want it to be clean only when I run my function.
When I press button in Matlab GUI I want to draw a line but when I pressed it second time I want to clear that line and draw other. How to simply do this.

 채택된 답변

David Sanchez
David Sanchez 2013년 9월 16일

1 개 추천

Implement this in the pushbutton:
children = get(gca, 'children');
delete(children(1));
The first line grabs the last additions to the figure, the second line deletes from the plot the last element added to the figure.

댓글 수: 3

Jonasz
Jonasz 2013년 9월 16일
But if I run this it will delate my main plot. When I delate it after drawing a line it's without sense because I need to display this line until I push again the button.
Jan
Jan 2013년 9월 16일
@Joansz: We cannot guess what you are doing exactly. Because the question is vague only, we can suggest some methods, but not a running piece of code. When you show us the relevant part of your code, more exactly answers are possible.
Jonasz
Jonasz 2013년 9월 16일
function my_list_Callback(hObject, eventdata, handles)
% hObject handle to my_list (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns my_list contents as cell array
% contents{get(hObject,'Value')} returns selected item from my_list
id=get(hObject,'Value');
list=get(hObject,'String');
item_selected=str2double(list{id});
di=get(handles.text_dir,'String');
[xmin,xmax,ymin,ymax,hm]=heatMap(item_selected,di);
%%Plot a line
if exist('dl', 'var')
delete(dl);
end
averSpec=load(fullfile('C:\Users\Jonasz\Documents\Studia\Biotechnologia\_Projekt\Workspace','averSpectrum.mat'));
averSpec=averSpec.averSpectrum;
axes(handles.axes_Spec);
hold on;
dl=plot([item_selected item_selected],[min(averSpec) max(averSpec)],'Color','red','LineWidth',1);
%%Plot a heatmap
cl=ymin:ymax;
rl=xmin:xmax;
hit=HeatMap(hm,'RowLabels',rl,'ColumnLabels',cl,'ColorMap',redbluecmap,'Symmetric',false);
guidata(hObject,handles);
This is my code. This is the function which is running when I press some value in my list. There are many values e.g. 1000 values. I want to draw each time this value ( line exactly in this value) on my plot but I want to refresh this line after I change the value in 'mylist'.

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

추가 답변 (1개)

Simon
Simon 2013년 9월 16일

1 개 추천

Hi!
Each time you plot something with the "h=plot" command, it gives you as return value the handle "h" of that plot. Then you may use
delete(h)
You just need to remember this handle (store it somewhere ...)

댓글 수: 4

Jonasz
Jonasz 2013년 9월 16일
I know it the problem is that I want to delate it only when I press the button ( run the function) When I use first delate(h) and then h=line... or h=plot it's without sense. I have to implement first handle h ?
Simon
Simon 2013년 9월 16일
First time you call your function the handle "h" does not exist. Thus you can write
if exist('h', 'var')
delete(h)
end
The line is deleted only if it exists.
Jonasz
Jonasz 2013년 9월 16일
It's okey but there is some problem.The variable h is only visible inside the function. How the function will know if the h exist or not?
Simon
Simon 2013년 9월 17일
So, in your code above you have everything you need. Storing a variable for future use is done using "guidata". See http://www.mathworks.com/help/matlab/ref/guidata.html?searchHighlight=guidata

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

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

제품

태그

질문:

2013년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by