Introduction

What Is Automation?

Automation is a COM protocol that allows one application (the controller or client) to control objects exported by another application (the server). MATLAB software on Microsoft Windows operating systems supports COM Automation server capabilities. Any Windows program that can be configured as an Automation controller can control MATLAB. Some examples of applications that can be Automation controllers are Microsoft Excel, Microsoft Access, and Microsoft Project applications, and many Microsoft Visual Basic and Microsoft Visual C++ programs.

Creating the MATLAB Server

To create a server, you need a programmatic identifier (ProgID) to identify the server. The ProgID for MATLAB is matlab.application. For other a list of MATLAB ProgIDs, see Programmatic Identifiers.

How you create an Automation server depends on the controller you are using. Consult your controller's documentation for this information.

If your controller is a MATLAB application and your server is another MATLAB application, you create the Automation server using the actxserver function:

h = actxserver('matlab.application')
h =
   COM.matlab.application

This command automatically creates the Automation server. You can also create the server manually. See Creating the Server Manually.

The following topics:

Using MATLAB Software as a Shared or Dedicated Server

The MATLAB Automation server has two modes:

If you use matlab.application as your ProgID, MATLAB creates a shared server. For information about creating shared and dedicated servers, see Specifying a Shared or Dedicated Server.

Accessing Your Server from the Startup Directory

The MATLAB Automation server starts up in the matlabroot\bin\win32 directory. If this is not the MATLAB client startup directory, the newly created server does not run the MATLAB startup file (startup.m) and does not have access to files in that directory.

To access files in the startup directory, do one of the following:

Get the Status of a MATLAB Automation Server

Use the enableservice function to determine the current state of a MATLAB Automation server. The function returns a logical value, where logical 1 (true) means MATLAB is an Automation server and logical 0 (false) means MATLAB is not an Automation server.

For example, if you type:

enableservice('AutomationServer')

and MATLAB displays:

ans =
     1

then MATLAB is currently an Automation server.

Creating a MATLAB Automation Server from Visual Basic .NET Application

If you use a Visual Basic client application to access a MATLAB Automation server, you have two options for creating the server:

Accessing Methods from the Visual Basic Object Browser.   You can use the Object Browser of your Visual Basic client application to see what methods are available from a MATLAB Automation server. To do this you need to reference the MATLAB type library in your Visual Basic project.

To set up your Visual Basic project:

  1. Select the Project menu.

  2. Select Reference from the subsequent menu.

  3. Check the box next to the MATLAB Application Type Library.

  4. Click OK.

In your Visual Basic code, use the New method to create the server:

Matlab = New MLApp.MLApp

View MATLAB Automation methods from the Visual Basic Object Browser under the Library called MLAPP.

Using CreateObject.   To use the Visual Basic CreateObject method, type:

MatLab = CreateObject("Matlab.Application")

Connecting to an Existing MATLAB Server

It is not always necessary to create a new instance of a MATLAB server whenever your application needs some task done in MATLAB. Clients can connect to an existing MATLAB Automation server using the actxGetRunningServer function or by using a command similar to the Visual Basic .NET GetObject command.

Using Visual Basic .NET Code

The Visual Basic .NET command shown here returns a handle h to the MATLAB server application:

h = GetObject(, "matlab.application")

The following Visual Basic .NET example connects to an existing MATLAB server, then executes a plot command in the server. If you do not already have a MATLAB server running, create one following the instructions in Creating a MATLAB Automation Server from Visual Basic .NET Application.

Dim h As Object
h = GetObject(, "matlab.application") 

' Handle h should be valid now. 
' Test it by calling Execute.
h.Execute ("plot([0 18], [7 23])")
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS