I have about 20 columns of data, each ~20,000 rows. All calculations are done in Matlab, and then charts are created.
Unfortunately, I need the whole output to be in an Excel File, including several editable charts. Using xlswrite
I created the Excel file, now I am struggling to create the charts the way I want.
I tried using this code, but got several errors and wasn't able to choose the X and Y Values of the chart.
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
The error:
Error using COM.Excel_Application/Range. Object returned error code: 0x800A03EC"
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.
Summary, my question is:
- How can I insert charts in Excel using Matlab (using a general, up to date ActiveX code structure).
- How can I select the columns for the XValue and the YValues (Range)
- How can I get access to chart titles, axes, line appearance and legend
I'm using Excel 2016 and Matlab R2017a.
Edit:
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:
Important is only chart1. I wanted to set the XValue to column B and the YValues to column H - P of the sheet1 (Tabelle1).
%%%%% 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;
another Error occurs:
Error using Interface.000208D8_0000_0000_C000_000000000046/Range
Error: Object returned error code: 0x800A03EC
Error in CodeTestmy (line 13) chart1.SetSourceData(wb.Worksheets.Item('Tabelle1').Range('$B:$B, $H:$P'));
for now this question is nearly answered. For further bug related answers have a look at a related question