MATLAB: How to clear existing figure in excel using matlab

active servergraph delete

I have an excel that is connected to matlab through active server. I am generating a graph via matlab and export it to excel. However, I need to clear the existing graphs first so it won't overwrite and make a stacked of graphs.
My idea is to find the existing graph first, and clear it. But, so far, I still don't know how to do it.
Anyone knows? I am open to any suggestions.
Thank you so much!
Elizabeth

Best Answer

Here is a more efficient version of your original code:
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(filename);
graph = Workbook.Sheets.Item('Graphs');
plotpie(cost, init_stage);
graph.Shapes.AddPicture('location', 0, 1, 400, 325, 345, 230);
To delete the picture you've added, and assuming that it is the only picture on the Graphs worksheet:
graph.Shapes.Item(1).Delete