MATLAB: How to programmatically set the elements of a chart object in Excel 2007 from MATLAB 7.8 (R2009a)

activexactxservercomexcelMATLABplot

I have an Excel file which has numerous sheets and each sheet has a chart object that is created in Excel. I wanted an example where MATLAB communicates with these Excel sheets and make changes to the title of the chart object in Excel sheet.

Best Answer

To access a chart object in Excel 2007 from MATLAB 7.8 (R2009a),
1) Start Excel as an ACTXSERVER application in MATLAB.
2) Open the Excel workbook.
3) Open the worksheet that contains the chart.
4) Get the Chart object to modify.
5) Implement the code that modifies the elements of the existing chart.
The following code provides an example on how to achieve this.
excel = actxserver('excel.application')
wkbk = excel.Workbooks.Open('Book1.xlsx') % open the excel file, full path need to be mentioned or else excel will pick it from most recently opened files.
wksheet = wkbk.Worksheets.Item('Sheet1') % Choose desired sheet
co = wksheet.ChartObjects.Item(1) % select the plot (this selects the first plot
co.Chart.ChartTitle.Text ='myplot1' % Change the title
wkbk.Save % save the changes
excel.Quit % close excel
Also, see the link below for the way to do it using Microsoft Visual Studio with C# and Visual Basic:
<http://msdn.microsoft.com/en-us/library/bb404904.aspx>