Using Matlab to produce Excel graphs

41 次查看(过去 30 天)
I want to use Matlab to take in some data, run analysis on it, and then output the resulting analysis into an excel spreadsheet with some graphs. I know how to input data into Matlab and I know how to do the analysis and write the resulting data/matrices from Matlab onto Excel, but I am having trouble using Matlab to plot the data on Excel. I have come across this code that has been of huge help and can plot the data I want, but I have some additional questions. How can I modify this code to:
  1. How can I make plots on the sheets where the data is located (So how do I change the positon of where the chart is located)?
  2. How can I plot multiple series on one graph?
  3. Can anyone point me to some documentation that is relevant when using actxserver function on Excel?
  4. Also can someone tell me what the actxserver function and invoke function in Matlab do? I read the description but I have no idea what an OLE Automation server and Dynamic Link Library are so I would greatly appreciate if someone can help me understand.
Thank you!!!
% Start Excel and open workbook
Excel = actxserver('Excel.Application');
WB = Excel.Workbooks.Open('C:\Users\Alberto\Desktop\test.xlsx');
% Show the workbook
Excel.visible = 1;
% Add chart
Chart = invoke(WB.Charts,'Add');
% Get Sheet object
SheetObj = Excel.Worksheets.get('Item', 'Sheet1');
% Name chart sheet
Chart.Name = 'TestChart';
% Set source data range of chart
% X and Y data can also be set to Matlab arrays, by Srs.Values and Srs.XValues, ensuring equal length
% Y data
Srs = Chart.SeriesCollection.Add(SheetObj.Range('B2:B16'));
% X data, could be a Matlab array of correct length
Srs.XValues = SheetObj.Range('A2:A16');
% Series name
Srs.Name = 'Test Series';
% For chart types, see https://msdn.microsoft.com/en-us/library/office/ff837417.aspx
Chart.ChartType = 'xlLine';
% Set chart title, see https://msdn.microsoft.com/en-us/library/office/ff196832.aspx
Chart.HasTitle = true;
Chart.ChartTitle.Text = 'Test Title';
% Set chart legend, see https://msdn.microsoft.com/en-us/library/office/ff821884.aspx
Chart.HasLegend = true;

回答(1 个)

Image Analyst
Image Analyst 2022-6-1
Invoke is just an older way of doing OOP code. So
hart = invoke(WB.Charts,'Add');
could probably be done like
hart = WB.Charts.Add;

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by