Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?

256 views (last 30 days)
I am trying to control Excel from MATLAB using ActiveX. I would like some examples that show how to use the ActiveX automation interface from Excel to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2021
Edited: MathWorks Support Team on 13 Dec 2021
This enhancement has been incorporated into the documentation for Release 14 Service Pack 3 (R14SP3). You can find an example in the "External Interfaces" manual in the chapter "COM and DDE Support (Windows Only)" under "Using MATLAB as an ActiveX Client" or on the web:
For previous product releases, please see the example below:
Most of the functionality that you get from ActiveX is dependent on the object model, which the external application implements. Consequently, we are usually unable to provide much information about the functions that you need to use in the remote application to perform a particular function. We do, however, have an example that shows how to perform common functions in Excel.
We also recommend that you become more familiar with the Excel object model in order to better use Excel's ActiveX automation interface from MATLAB. You can find more information on this interface by selecting the "Microsoft Excel Visual Basic Reference" topic in the Microsoft Excel Help Topic dialog. This topic area contains a searchable description of Excel methods and properties.
The following example demonstrates how to insert MATLAB data into Excel. It also shows how to extract some data from Excel into MATLAB. For more information, refer to the individual comments for each code segment.
% Open Excel, add workbook, change active worksheet,
% get/put array, save, and close
% First open an Excel Server
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the second sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');
% Get a handle to the active sheet
Activesheet = Excel.Activesheet;
% Put a MATLAB array into Excel
A = [1 2; 3 4];
ActivesheetRange = get(Activesheet,'Range','A1:B2');
set(ActivesheetRange, 'Value', A);
% Get back a range. It will be a cell array,
% since the cell range can
% contain different types of data.
Range = get(Activesheet, 'Range', 'A1:B2');
B = Range.value;
% Convert to a double matrix. The cell array must contain only scalars.
B = reshape([B{:}], size(B));
% Now save the workbook
invoke(Workbook, 'SaveAs', 'myfile.xls');
% To avoid saving the workbook and being prompted to do so,
% uncomment the following code.
% Workbook.Saved = 1;
% invoke(Workbook, 'Close');
% Quit Excel
invoke(Excel, 'Quit');
% End process
delete(Excel);
There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, refer to the "Related Solutions" below.
For an example that shows how to connect MATLAB with Excel using DDE, refer to the "Related Solutions" below.
For information on how to use the XLSREAD function to read .xls files, please refer to the following URL:
  4 Comments
ERIKA PIERONI
ERIKA PIERONI on 5 May 2022 at 12:04
Hi @Image Analyst and @MathWorks Support Team, I'm using your Demo as an example for my project.
I would like to know how, when I copy-paste a spreadsheet, to break all the external links, or change them.
My guess is that I should implement in matlab the equivalent of (in VBA):
ActiveWorkbook.ChangeLink "c:\excel\book1.xls", _
"c:\excel\book2.xls", xlExcelLinks
or
ActiveWorkbook.BreakLink Name:=ExternalLinks(x), Type:=xlLinkTypeExcelLink
or
Excel.ActiveSheet.Paste Link:=true
but I do not know how to encode in matlab the optionsof type
Link:= True
Thank you for your help :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!