How to insert a Chart in Excel through Matlab
33 次查看(过去 30 天)
显示 更早的评论
I know there are many old posts about this topic, but none was able to help me, or outdated.
My project involves about 20 columns of Data, each 20.000 rows. All Calculations in Matlab are done and charts created.
But unfortunatly I need the whole evaluation in an Excel File, including several editable Charts.
Using xlswrite I created the Excel Files, now I struggle to create the Charts the way I like.
I tryed using this code, but got several errors and wasn't able to choose the X and Y Values of the Chart.
if true
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 "Error using
COM.Excel_Application/Range
Error: Object returned error code: 0x800A03EC"
end
I searched the web for hours how to use ActiveX to add Charts, but no suggestion worked for me.
So my question is, to make it clear:
- How to insert Charts in Excel using Matlab (general up to date ActiveX code structure)
- How to select own columns for the XValue and the YValues (Range)
- How to get acces to Chart Titles, Axes, Line appearance and Legend
Can anyone please help me out? Thank you a lot!
Kilian
By the way, I'm using latest Excel 2016, and Matlab R2017a. I need this for a project in an internship during college.
0 个评论
回答(1 个)
Guillaume
2017-6-13
编辑:Guillaume
2017-6-13
Not directly relevant to your question: I often see questions where people use invoke and get when dealing with COM. In most cases it's not needed; your code would work just as well (or as bad) with:
excel = actxserver('Excel.Application');
wb = excel.Workbooks.Open(somefile);
charts = wb.Charts;
chart = charts.Add;
chart.SetSourceData(excel.Range('Sheet1!$B$2:$B$16'));
and is easier to read and gives you tab completion.
Relevant to your question: I get no error and a chart with the above code, when testing against an excel file picked at random. This is using excel 2013 but I don't believe 2016 changed anything in that respect. Matlab is R2017a but that does not matter for your problem.
First thing to know is where the error is coming from, the range creation or setSourceData, so decouple that last line into:
rg = excel.Range('Sheet1!$B$2:$B$16');
chart.SetSourceData(rg);
and tell us which line causes the error. I would also recommend
excel.Visible = true;
after you've created the excel object, so you can see what is actually happening.
Finally, if you can attach you workbook (or a dummy one that causes the error) that would help.
16 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!