Main Content

Create MATLAB Server

Note

Instead of writing applications that call MATLAB® as a COM Automation server, consider using the MATLAB Engine API for .NET. For more information, see Call MATLAB from .NET.

Choose ProgID

To create a server, you need a programmatic identifier (ProgID) to identify the server. MATLAB has ProgIDs for shared and dedicated servers. These IDs are either version specific or version independent.

The MATLAB ProgIDs for shared servers are:

  • Matlab.Application — Starts a command window Automation server with the version of MATLAB that was most recently used as an Automation server (which might not be the latest installed version of MATLAB)

  • Matlab.Autoserver — Starts a command window Automation server using the most recent version of MATLAB

  • Matlab.Desktop.Application — Starts the full desktop MATLAB as an Automation server using the most recent version of MATLAB

The ProgIDs for dedicated servers are:

  • Matlab.Application.Single

  • Matlab.Autoserver.Single

These version-independent MATLAB ProgIDs specify the currently registered version of MATLAB.

To create an instance of a specific registered MATLAB version, you can use a version-dependent ProgID. For example, Matlab.Application.7.14 creates an instance of MATLAB version 7.14 (R2012a).

Create Automation Server

Your client application establishes a connection to the MATLAB server. How you create the connection depends on the language of your client program. Consult the language documentation for this information. Possible options include:

  • C# client:

    mlType = Type.GetTypeFromProgID("Matlab.Application");
    matlab = Activator.CreateInstance(mlType);
    

    where mlType and matlab are defined as:

    public static Type mlType;
    public static Object matlab;
  • Visual Basic® .NET client:

    MatLab = CreateObject("Matlab.Application")

    where MatLab is defined as:

    Dim MatLab As Object
  • VBA client:

    Set MatLab = CreateObject("matlab.application")

    where MatLab is defined as:

    Dim MatLab As Object

Start MATLAB as Automation Server in Desktop Mode

This Microsoft® Visual Basic .NET code starts MATLAB as a COM Automation server in full desktop mode using the ProgID Matlab.Desktop.Application.

Dim MatLab As Object
Dim Result As String
MatLab = CreateObject("Matlab.Desktop.Application")
Result = MatLab.Execute("surf(peaks)")

Connect to Existing MATLAB Server

It is not always necessary to create a new instance of a MATLAB server. Clients can connect to an existing MATLAB Automation server using language-specific commands. For example, this Visual Basic .NET example connects to an existing MATLAB server, then executes a plot command in the server.

Dim h As Object
h = GetObject(, "matlab.application") 
h.Execute ("plot([0 18], [7 23])")

Note

Use the GetObject syntax shown, which omits the first argument.

Alternatively, you can specify a running session of MATLAB as a COM server. For more information, see Manually Create Automation Server.

Control MATLAB Appearance on Desktop

You can make MATLAB visible on the desktop by setting the Visible property. When visible, MATLAB appears on the desktop, enabling the user to interact with it. This might be useful for such purposes as debugging. The Visible property is enabled (set to 1) by default.

When not visible, the MATLAB window does not appear, which prevents interaction with the application. To hide the desktop, set the Visible property to 0.

This Visual Basic .NET code shows how to disable the Visible property.

Dim MatLab As Object
MatLab = CreateObject("matlab.application")
MatLab.Visible = 0

Related Examples

More About