How to Plot data in excel sheet using MATLAB ?

Illustration
Divyesh pandav - 2022-11-11T10:40:42+00:00
Question: How to Plot data in excel sheet using MATLAB ?

                              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

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

To create the bar chart inside the excel file, you will need to use the ActiveX interface to talk to Excel on MS Windows (will not work on Mac or Linux)
 
There are two approaches.
 
One approach is to create a graph at the MATLAB level, and convert the graph to an image, and insert that image into the excel file.  See for an example.

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


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!