| MATLAB® | ![]() |
| On this page… |
|---|
Using a MATLAB® Application as an Automation Client Connecting to an Existing Excel® Application |
MATLAB® software ships with a simple example COM control that draws a circle on the screen, displays some text, and fires events when the user single- or double-clicks the control. Create the control by running the mwsamp.m file in the directory, winfun\comcli, or type:
h = actxcontrol('mwsamp.mwsampctrl.2', [0 0 300 300]);You can find this control in the MATLAB bin, or executable, directory along with the control's type library. The type library is a binary file used by COM tools to decipher the control's capabilities. For other examples using the mwsamp2 control, see Writing Event Handlers.
This example uses MATLAB software as an Automation client and the Microsoft® Excel® spreadsheet program 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.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
% Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item', 1);
eSheet1.Activate;
% Put a MATLAB array into Excel.
A = [1 2; 3 4];
eActivesheetRange = e.Activesheet.get('Range', 'A1:B2');
eActivesheetRange.Value = A;
% Get back a range.
% It will be a cell array, since the cell range
% can contain different types of data.
eRange = e.Activesheet.get('Range', 'A1:B2');
B = eRange.Value;
% Convert to a double matrix. The cell array must contain only
% scalars.
B = reshape([B{:}], size(B));
% Now, save the workbook.
eWorkbook.SaveAs('myfile.xls');
% Avoid saving the workbook and being prompted to do so
eWorkbook.Saved = 1;
eWorkbook.Close;
% Quit Excel and delete the server.
e.Quit;
e.delete;Note Make sure that you always close any workbooks you create. This can prevent potential memory leaks. |
You can give MATLAB access to a file that is open by another application by creating a new COM server from the MATLAB client, and then opening the file through this server. This example shows how to do this for an Excel® application that has a file weekly_log.xls open:
excelapp = actxserver('Excel.Application');
wkbk = excelapp.Workbooks;
wdata = wkbk.Open('d:\weatherlog\weekly_log.xls');To see what methods are available, type:
wdata.methods
Methods for class Interface.Microsoft_Excel_10.0_
Object_Library._Workbook:
AcceptAllChanges LinkInfo ReloadAs
Activate LinkSources RemoveUser
: : :
: : :Access data from the spreadsheet by selecting a particular sheet (called 'Week 12' in the example), selecting the range of values (the rectangular area defined by D1 and F6 here), and then reading from this range:
sheets = wdata.Sheets;
sheet12 = sheets.Item('Week 12');
range = sheet12.get('Range', 'D1', 'F6');
range.value
ans =
'Temp.' 'Heat Index' 'Wind Chill'
[78.4200] [ 32] [ 37]
[69.7300] [ 27] [ 30]
[77.6500] [ 17] [ 16]
[74.2500] [ -5] [ 0]
[68.1900] [ 22] [ 35]
wkbk.Close;
excelapp.Quit;In the following example, MATLAB runs the Microsoft Excel spreadsheet program in a COM server and invokes a macro that has been defined within the active Excel spreadsheet file. The macro, init_last, takes no input parameters and is called from the MATLAB client using the statement:
handle.ExecuteExcel4Macro('!macroname()');Start the example by opening the spreadsheet file and recording a macro. The macro used here simply sets all items in the last column to zero. Save your changes to the spreadsheet.
Next, in MATLAB, create a COM server running an Excel application, and open the spreadsheet:
h = actxserver('Excel.Application');
wkbk = h.Workbooks;
file = wkbk.Open('d:\weatherlog\weekly.xls');Open the sheet that you want to change, and retrieve the current values in the range of interest:
sheets = file.Sheets;
sheet12 = sheets.Item('Week 12');
range = sheet12.get('Range', 'D1', 'F5');
range.Value
ans =
[ 78] [ 32] [ 37]
[ 69] [ 27] [ 30]
[ 77] [ 17] [ 16]
[ 74] [ -5] [ -1]
[ 68] [ 22] [ 35]Now execute the macro, and verify that the values have changed as expected:
h.ExecuteExcel4Macro('!init_last()');
range.Value
ans =
[ 78] [ 32] [ 0]
[ 69] [ 27] [ 0]
[ 77] [ 17] [ 0]
[ 74] [ -5] [ 0]
[ 68] [ 22] [ 0]MATLAB includes a demo illustrating the use of the COM Client with MATLAB. To run the demo, click the Demos tab in the MATLAB Help browser. Click to expand the folder called External Interfaces and select Programming with COM.
![]() | Handling COM Data in MATLAB® Software | Advanced Topics | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |