a- 1 a-2 a-3 aaa 1 6 15 bbb 2 7 14 ccc 3 8 13 ddd 4 9 12 eee 5 10 11
John Williams answered .
2025-11-20
You can save the graph created with this interface in an Excel file. This example uses a separate Excel Automation server process for this purpose. The callback for the Save Graph push button creates the image and adds it to an Excel file:
Both the axes and legend are copied to an invisible figure configured to print the graph as you see it on the screen (figure PaperPositionMode property is set to auto).
The print command creates the PNG image.
Use the Shapes interface to insert the image in the Excel workbook.
The server and interfaces are instanced during the initialization phase:
exl2 = actxserver('excel.application');
exlWkbk2 = exl2.Workbooks;
wb = invoke(exlWkbk2,'Add');
graphSheet = invoke(wb.Sheets,'Add');
Shapes = graphSheet.Shapes;
Use this code to implement the Save Graph button callback:
function saveButtonCallback(src,evt)
tempfig = figure('Visible','off','PaperPositionMode','auto');
tempfigfile = [tempname '.webp'];
ah = findobj(f,'type','axes');
copyobj(ah,tempfig) % Copy both graph axes and legend axes
print(tempfig,'-dpng',tempfigfile);
Shapes.AddPicture(tempfigfile,0,1,50,18,300,235);
exl2.visible = 1;
end
The other approach is to use the ActiveX interface to send VBA plotting commands to Excel. See an example
https://www.matlabsolutions.com/resources/plots-in-excel-sheets-using-matlab.php