How can I add multiple charts in excel with activex
显示 更早的评论
I've been writing a script that needs to add up to a hundred or so excel charts in one worksheet... I'm using activex to access excel through matlab and I'm using the Excel.ActiveSheet.ChartObjects.Add(Left,Top,Width,Height) function. One problem though, it won't seem to let me add sheets when either the 'left' or 'top' position is greater than a certain number, around 250-350. Is there anyway to fix this?
Many thanks, Mirage
7 个评论
Eric
2012-10-25
I don't seem to have this problem. Here's what I tried:
xlsobj = actxserver('Excel.Application');
xlsobj.Visible = 1;
WB_COM = xlsobj.Workbooks.Add
Sheet = WB_COM.Sheets.Item(1);
chart1 = Sheet.ChartObjects.Add(100,100,200,200);
chart2 = Sheet.ChartObjects.Add(100,300,200,200);
chart3 = Sheet.ChartObjects.Add(100,900,200,200);
chart4 = Sheet.ChartObjects.Add(900,900,200,200);
All four of these charts were created on the desired worksheet. I'm using Excel 2010 on Windows 7 x64 with Matlab R2012b.
Sorry I can't be of more help.
-Eric
Mirage
2012-10-25
Eric
2012-10-26
I wonder if the problem is in how you're calling this function. I added the following code at the top of your function to test what you've written. Here's what I added:
Excel = actxserver('Excel.Application');
Excel.Visible = 1;
Excel.Workbooks.Add;
PageIdx = 1;
Left = 1000;
Top = 1000;
Width = 100;
Height = 100;
I then ran your code below and it works fine:
ExcelWs = Excel.Workbooks;
XLSheet = Excel.ActiveWorkBook.Sheets;
GetXLSheet = get(XLSheet, 'Item', PageIdx);
invoke(GetXLSheet,'Activate');
XLActive = Excel.ActiveSheet;
Excel.Visible = 0;
XLChartObject = XLActive.ChartObjects.Add(Left,Top,Width,Height);
I did set Excel.Visible to 1 after running this code to check that the graph was created correctly (and it was).
This makes me think that perhaps your problem is in the calling of this function rather than this function itself. Perhaps you're not passing in the values for Left and Top that you think you're passing in?
As an aside, I always tell people to be careful when using things like ActiveSheet, ActiveWorkBook, etc. If the user does anything to Excel that changes what is "active" your code will run into problems (i.e., start making changes to the wrong sheet or workbook).
Good luck,
Eric
Eric
2012-10-26
I created a dummy test spreadsheet to test this out. My usage code is:
Excel = actxserver('Excel.Application');
WB = Excel.Workbooks.Open(fullfile(pwd,'xlsgraphchart.xlsx'));
LinSlope, PolySlope, LinSlope_Str, PolySlope_Str] = xlsgraphchart(Excel, 'A1:A10','B1:B10','X','Y',10000,10000,1000,1000,1);
For the Lin_y_Eq and Lin_R_Squared strings I get:
K>> Lin_y_Eq
Lin_y_Eq =
y = 3x + 2
K>> Lin_R_Squared
Lin_R_Squared =
R² = 1
These are each correct. I've now looked a bit further at what you're doing. I would probably actually do the fitting in Matlab rather than Excel. Alternatively, you might be able to use the COM interface to Excel to perform the calculations using Excel functions rather than reading values off of the plot strings. That might simplify things. I'll look at this a bit more.
-Eric
Mirage
2012-10-26
I wonder if Matlab or Excel is choking on the superscript "2" character somehow. I was surprised to see that print out.
You can see my answer for how to easily calculate R^2 for the linear fit without the need for reading from the trendline label. The polynomial fit is a bit trickier.
-Eric
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!