External Interfaces |
Examples of MATLAB as an Automation Client
This section provides examples of using MATLAB as an Automation client with controls and servers:
MATLAB 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 on the control. Create the control by running the mwsamp.m
file in the directory, winfun\comcli
, or type
This control is stored 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. See the section Writing Event Handlers for other examples that use the mwsamp2
control.
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. 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'); % To avoid saving the workbook and being prompted to do so, % uncomment the following code. % eWorkbook.Saved = 1; % eWorkbook.Close; % Quit Excel and delete the server. % e.Quit; % e.delete;
Note Make sure that you always close any workbooks that you add in Excel. This can prevent potential memory leaks. |
Connecting to an Existing Excel Application
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 you have available to you, 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;
Running a Macro in an Excel Server Application
In the example below, MATLAB runs Microsoft Excel 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
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]
Identifying Objects | Additional COM Client Information |
© 1994-2005 The MathWorks, Inc.