This example creates a COM server application running Excel. The returned handle is assigned to h: h = actxserver('excel.application')
h =
COM.excel.application
Once an object has been created, you can obtain interfaces from it, either by assigning an Interface property from the object or by invoking certain methods on it. This example creates a Workbooks interface to the Excel application using the get function. The get function returns an interface handle that is assigned here to w. Use this handle in other COM function calls as a reference to this interface: w = get(h, 'Workbooks')
w =
Interface.excel.application.Workbooks
Excel Link Versions By default, Excel Link (a separately orderable product) supports two versions of Excel. This table lists which Excel Link files to use with each Excel version. Use the appropriate files for your version of Excel. You can find these files in the $MATLAB\toolbox\exlink subdirectory.
Excel Version Excel Link
FileExcel 97 (Default) excllink.xla
ExliSamp.xls
Excel 7 excllink95.xla
ExliSamp95.xls
Using MATLAB as an Automation Client
This example uses MATLAB as an Automation client and Microsoft Excel as the server. It provides a good overview of typical functions. In addition, it is a good example of using the Automation interface of another application: % MATLAB Automation client example
%
% Open Excel, add workbook, change active worksheet,
% get/put array, save.
% First, open an Excel Server.
e = actxserver('excel.application');
% Insert a new workbook.
eWorkbooks = get(e, 'Workbooks');
eWorkbook = Add(eWorkbooks);
set(e, 'Visible', 1);
% Make the second sheet active.
eActiveWorkbook = get(e, 'ActiveWorkBook');
eSheets = get(eActiveWorkbook, 'Sheets');
eSheet2 = Item(eSheets, 2);
Activate(eSheet2);
% Get a handle to the active sheet.
eActiveSheet = get(e, 'ActiveSheet');
% Put a MATLAB array into Excel.
A = [1 2; 3 4];
eActiveSheetRange = Range(eActiveSheet, 'A1', 'B2');
set(eActiveSheetRange, 'Value', A);
% Get back a range. It will be a cell array, since the cell range
% can contain different types of data.
eRange = Range(eActiveSheet, 'A1', 'B2');
B = get(eRange, 'Value');
% Convert to a double matrix. The cell array must contain only
% scalars.
B = reshape([B{:}], size(B));
% Now, save the workbook.
SaveAs(eWorkbook, 'myfile.xls');
% To avoid saving the workbook and being prompted to do so,
% uncomment the following code.
% set(eWorkbook, 'Saved', 1);
% Close(eWorkbook);
% Quit Excel and delete the server.
% Quit(e);
% delete(e);
Data Management Functions
Excel Link provides nine data management functions to copy data between
Excel and MATLAB and to execute MATLAB commands from Excel.
matlabfcnEvaluate MATLAB command given Excel data.
matlabsubEvaluate MATLAB command given Excel data and designate output location.
MLAppendMatrixCreate or append MATLAB matrix with data from Excel worksheet.
MLDeleteMatrixDelete MATLAB matrix.MLEvalStringEvaluate command in ATLAB.
MLGetMatrixWrite contents of MATLAB matrix in Excel worksheet.
MLGetVarWrite contents of MATLAB matrix in Excel VBA (Visual Basic for Applications) variable.
MLPutMatrixCreate or overwrite MATLAB matrix with data from Excel worksheet.
MLPutVarCreate or overwrite MATLAB matrix with data from Excel VBA variable.
You can invoke any data management function except MLGetVar and MLPutVar as a worksheet cell formula or in a macro. You can invoke MLGetVar and MLPutVar only in a macro. Use MLAppendMatrix, MLPutMatrix, and MLPutVar to copy data from Excel
to MATLAB. Use MLEvalString to execute MATLAB commands from Excel. Use MLDeleteMatrix to delete a MATLAB variable. Use matlabfcn, matlabsub, MLGetMatrix and MLGetVar to copy data from MATLAB to Excel.