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
PaperPositionModeproperty is set toauto). -
The
printcommand creates the PNG image. -
Use the
Shapesinterface 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