MATLAB: How to delete figure in an excel file

actxserverdelete-figureexcel

Hi,
I would like to directly delete all the figures in an excel file from MATLAB. I can select all the figures using activex but I cant' figure out a way to delete them.
My code:
filename_out = 'Libraries\Documents\TEST.xlsx'; % filename
Excel = actxserver('Excel.Application'); % open the connection
set(Excel,'Visible',1);
Excel.Workbooks.Open(filename_out); % open excel file
worksheets = Excel.sheets;
numSheets = worksheets.Count; % count the number of sheets
for s = 1 : numSheets % do a loop for all sheets
worksheets.Item(s).Shapes.SelectAll; % select the figure
  • % How to delete selection? *
end
Thanks for any help!

Best Answer

I finally found a solution:
worksheets = Excel.sheets;
numSheets = worksheets.Count;
for s = 1 : numSheets % Delete all shapes in Excel
myshapes = worksheets.Item(s).Shapes;
for j = myshapes.Count:-1:1
myshapes.Item(j).Delete
end
end