MATLAB: How Can I change the range of a excel chart using actxserver

actxserver

This is my code:
excel = actxserver('Excel.Application');
excel.Visible = true;
excel.DisplayAlerts = false;
filename13 = 'C:\Dropbox\Idenglobal\Comparacion peers.xlsx';
pee = excel.Workbooks.Open(filename13);
peesheet = pee.Sheets.Item('Shortlist Credit Suisse');
In that sheet there is only a chart whith datas in the range "Q2:W67" . I would like to extend this range to "Q2:W69". How Can I do? Many thanks!!!

Best Answer

As per the description, you would like to change the data range for an Excel Chart using actxserver in MATLAB. Below is a code snippet that achieves the same.
excel = actxserver('Excel.Application');
excel.Visible = true;
excel.DisplayAlerts = false;
filename13 = 'C:\Dropbox\Idenglobal\Comparacion peers.xlsx';
pee = excel.Workbooks.Open(filename13);
peesheet = pee.Sheets.Item('Sheet1');
pC = peesheet.ChartObjects(1).Chart
pC.SeriesCollection(1).Values = peesheet.Range('W2:W69');
pC.SeriesCollection(1).XValues = peesheet.Range('Q2:Q69');
In the above code snippet Values corresponds to Y axis range of values. Similarly XValues corresponds to X axis range of values. By changing the range to 69, the previous range will be reset.
Also,I am assuming this chart will be the first chart in the Excel sheet selected (peesheet.ChartObjects(1).Chart).
Refer to the link below for more information on Series object properties: https://msdn.microsoft.com/en-us/library/office/ff838988.aspx