Before you plot, you should save your figure handle into a variable so that you can put it as an input of saveas function.
Instead of using gcf, assigning handles into variables is useful and prevents conflicts when you are dealing with multiple plots with multiple figures.
(Because gcf only returns the last focused figure object)
Here is example:
hFig = figure;
x = 0:10;
y = 10:20;