Using Matlab to produce Excel graphs
35 views (last 30 days)
Show older comments
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:
- 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)?
- How can I plot multiple series on one graph?
- Can anyone point me to some documentation that is relevant when using actxserver function on Excel?
- 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;
0 Comments
Answers (1)
Image Analyst
on 1 Jun 2022
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;
0 Comments
See Also
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!