External Interfaces Previous page   Next Page

Examples of MATLAB as an Automation Client

This section provides examples of using MATLAB as an Automation client with controls and servers:

MATLAB Sample Control

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:

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:

To see what methods you have available to you, type

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:

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:

Open the sheet that you want to change, and retrieve the current values in the range of interest:

Now execute the macro, and verify that the values have changed as expected:


Previous page  Identifying Objects Additional COM Client Information Next page

© 1994-2005 The MathWorks, Inc.