title: Using Matlab to create Excel charts with X and Y values from range
body: <p>I have about 20 columns of data, each ~20,000 rows. All calculations are done in Matlab, and then charts are created.</p>
  <p>Unfortunately, I need the whole output to be in an Excel File, including several editable charts. Using <code>xlswrite</code> I created the Excel file, now I am struggling to create the charts the way I want.</p>
  <p>I tried using this code, but got several errors and wasn't able to choose the X and Y Values of the chart.</p>
  <pre class="lang-matlab prettyprint-override"><code> Excel = actxserver('Excel.Application');  
   WB = Excel.workbooks.Open('D:\...\Test.xlsx');
   Charts = WB.Charts; 
   Chart = invoke(Charts,'Add');
   invoke(Chart, 'SetSourceData', Excel.Range('Sheet1!$B$2:$B$16')); %% here an error occurs 
  </code></pre>
  <p>The error:</p>
  <blockquote>
  <p>Error using COM.Excel_Application/Range. Object returned error code: 0x800A03EC&quot;</p>
  </blockquote>
  <p>I have not been able to find any solutions for adding charts through the ActiveX object in Matlab. Any posts I have found on this topic were outdated or unhelpful.</p>
  <hr />
  <p><strong>Summary</strong>, my question is:</p>
  <ul>
  <li>How can I insert charts in Excel using Matlab (using a general, up to date ActiveX code structure).</li>
  <li>How can I select the columns for the XValue and the YValues (Range)</li>
  <li>How can I get access to chart titles, axes, line appearance and legend</li>
  </ul>
  <hr />
  <p>I'm using Excel 2016 and Matlab R2017a.</p>
  <hr />
  <p><strong>Edit:</strong></p>
  <p>during this week I developed a own solution, that still doesn't work perfectly but is close to what I want. Could you have a look on this code too please:</p>
  <p>Important is only chart1. I wanted to set the <strong>XValue to column B</strong> and the <strong>YValues to column H - P</strong> of the sheet1 (Tabelle1).</p>
  <pre class="lang-matlab prettyprint-override"><code>%%%%% general Code to insert a Chart in Excel using Matlab %%%%%
  %% start Excel and open Workbook
  excel = actxserver('Excel.Application');
  wb = excel.Workbooks.Open('C:\...\Test.xlsx');
  %% makes the created sheet visible
  excel.Visible = true;
  %% add 1. Chart
  chart1 = wb.Charts.Add;
  %% set source data 
  chart1.SetSourceData(wb.Worksheets.Item('Tabelle1').Range('$B:$B, $H:$P')); % 'Tabelle1' is the german equal to sheet1, my excel is german
  %% Name chart sheet
  chart1.Name = '1. TestChart';
  %% Set chart title,  see https://msdn.microsoft.com/en-us/library/office/ff196832.aspx
  chart1.HasTitle = true;
  chart1.ChartTitle.Text = 'Test Title';
  %% Set chart types,  see https://msdn.microsoft.com/en-us/library/office/ff837417.aspx
  chart1.ChartType = 'xlXYScatterSmoothNoMarkers';
  %% Set chart legend, see https://msdn.microsoft.com/en-us/library/office/ff821884.aspx
  chart1.HasLegend = true;
  %% Set Axes Titles
  chart1.Axes(1).HasTitle = true;
  chart1.Axes(1).AxisTitle.Text = 'Time [s]'; % XAxes
  chart1.Axes(2).HasTitle = true;
  chart1.Axes(2).AxisTitle.Text = 'Temperature[°C]'; %YAxes
  %% add 2nd chart
  chart2 = wb.Charts.Add([], chart1); %place after chart1
  chart2.SetSourceData(wb.Worksheets.Item('Tabelle1').Range('$B:$B, $Q:$Q'));
  % ... same procedure as above
  
  %% use to quit all open Excel processes
  % excel.Quit;
  </code></pre>
  <p>another Error occurs:</p>
  <blockquote>
  <p>Error using Interface.000208D8_0000_0000_C000_000000000046/Range</p>
  <p>Error: Object returned error code: 0x800A03EC</p>
  <p>Error in CodeTestmy (line 13)
  chart1.SetSourceData(wb.Worksheets.Item('Tabelle1').Range('$B:$B, $H:$P'));</p>
  </blockquote>
  <hr />
  <p>for now this question is nearly answered. For further bug related answers have a look at a <a href="https://stackoverflow.com/questions/44632268/unsolved-bug-in-creating-charts-in-excel-freely-using-matlab">related question</a></p>