MATLAB: Clear a line drawn in plot.

MATLABplot

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.

Best Answer

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.