Creating an XY Scatter plot with ActX

36 views (last 30 days)
Robert Guldi
Robert Guldi on 29 May 2021
编辑: Allen on 1 Jun 2021
I'm trying to make a scatter plot of the A and B columns in an Excel Spreadsheet using ActX. However I am stuggling to use the invoke function, more specifically
Indoke(excel.activechart.SeriesCollection,“新闻”);
I've been having trouble with the invoke function from the start and have been doing my best to avoid it, as it seems like maybe it isn't compatible with R2021a (?), but I am struggling to find any sort of documentation to help me make the XY Scatter other than a few questions posted in 2014. Is there some place where I can find the documentation that I can't seem to find myself, or some other way to create a series of data on the chart? Thanks in advance for any help you might have.

Answers (1)

Allen
Allen 2021年5月30日
编辑:Allen on 1 Jun 2021
以下方法类似于调用 invoke , but does not expressly call the invoke 功能。还提供了一些其他选项,以制定图表。
%创建ActiveX com服务器和Intialize对象
Excel = actxserver('Excel.Application');
wb = excel.workbooks.s.open('您的excel filename.xlsx');% Opens the desired Excel file
excel.windowstate ='xlMaximized';%最大化屏幕的窗口
Excel.Visible = 1;%将Excel文件设置为可见。默认值是隐藏的。
excel.displayAlerts = 0;% Turns off Excel warnings
%创建缩​​写的手柄。没有必要,但有用。
ws = wb.worksheet;
wc = wb.charts;
%添加了带有指定名称的新图表表,然后选择图表表
Chart = WC.Add([],WS.Item(WS.Count));
Chart.Name =“Chartsheet名称”;
WC.Item(Chart.Name).Select;
%添加数据系列。根据需要更改Excel范围
nsx = excel.activechart.seriescollection.newseries;
表='your worksheet name containing data to add to scatterplot';
NSx.XValues = ['=''',sheet,''!$ a $ 2:$ a $ 11'];代表X-DATA的Excel范围的%字符串
nsx.values = ['=''',sheet,''!$ b $ 2:$ b $ 11'];% 细绳representing the excel range with Y-data
% Changes the chart type to a scatter plot with markers
Excel.ActiveChart.ChartType ='xlxyscatterlines';当添加其他系列绘图时,只需要一次调用一次
%其他系列格式选项
nsx.name ='Your Series Name';% 细绳
nsx.format.line.Wewert = 1.0;% Line weight
nsx.markerstyle ='xlmarkerstylecircle';%标记类型
NSx.Format.Line.ForeColor.RGB = RGB([red,green,blue],'excel');% Line color [0-255]
NSX.MarkerbackgroundColor = 0;% Marker Color (black)
nsx.markersize = 4;% Marker Size
% Assigns descriptions for the axes and chart titles
%设置X轴
XAxis = Excel.ActiveChart.Axes(1);
XAxis.HasTitle = 1;
XAxis.AxisTitle.Caption =“时间(SEC)”;
xaxis.ticklabelposition ='xlticklabelpositionlow';
xaxis.hasmajorgridlines = 1;
xaxis.minimumScale = -suplim;
XAxis.MaximumScale = SupLim;
%设置Y轴
YAxis = Excel.ActiveChart.Axes(2);
YAxis.HasTitle = 1;
yaxis.axistitle.caption ='Acceleration (g)';
YAxis.HasMajorGridlines = 1;
yaxis.minimumScale = xlim(1);
yaxis.maximumScale = xlim(2);
%设置图表标题
excel.activechart.hastitle = 1;
excel.activechart.charttitle.characters.text = titxt;
% Change legend position. Legend requires manual fontsize change.
excel.activechart.legend.position ='xllegendPositionTop';
%保存并关闭Excel文件
excel.activeworkbook.save;
excel.quit;
Excel.delete;
2 Comments
Allen
Allen on 1 Jun 2021
@Robert Guldi the error you are getting is likely because there are no chartsheets in the specified workbook. That line is trying to add a new chartsheet after the last chartsheet, but will error if none are present. You can replace with the following to add a chartsheet after the last worksheet instead.
Chart = WC.Add([],WS.Item(WS.Count));
我更新了我的答案use this line instead and also remove the portion that was creating a new worksheet named 'delete'. That was a left over bit from one of my scripts that I forgot to remove and is not necessary.

Sign in to comment.

社区寻宝

Find the treasures in MATLAB Central and discover how the community can help you!

开始狩猎!