This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create a C# Client

This example shows how to call a deployed MATLAB® function from a C# application using MATLAB Production Server™.

In your C# code, you must:

  • Create a Microsoft® Visual Studio® Project.

  • Create a Reference to the Client Run-Time Library.

  • Design the .NET interface in C#.

  • Write, build, and run the C# application.

This task is typically performed by .NET application programmer. This part of the tutorial assumes you have Microsoft Visual Studio and .NET installed on your computer.

Create a Microsoft Visual Studio Project

  1. Open Microsoft Visual Studio.

  2. Click File > New > Project.

  3. In the New Project dialog, select the project type and template you want to use. For example, if you want to create a C# Console Application, select Windows in the Visual C# branch of the Project Type pane, and select the C# Console Application template from the Templates pane.

  4. Type the name of the project in the Name field (Magic, for example).

  5. Click OK. Your Magic source shell is created, typically named Program.cs, by default.

Create a Reference to the Client Run-Time Library

Create a reference in your MainApp code to the MATLAB Production Server client run-time library. In Microsoft Visual Studio®, perform the following steps:

  1. In the Solution Explorer pane within Microsoft Visual Studio (usually on the right side), select the name of your project, Magic, highlighting it.

  2. Right-click Magic and select Add Reference.

  3. In the Add Reference dialog box, select the Browse tab. Browse to the MATLAB Production Server client runtime, installed at $MPS_INSTALL\client\dotnet. Select MathWorks.MATLAB.ProductionServer.Client.dll.

  4. Click OK. MathWorks.MATLAB.ProductionServer.Client.dll is now referenced by your Microsoft Visual Studio project.

Design the .NET Interface in C#

In this example, you invoke mymagic.m, hosted by the server, from a .NET client, through a .NET interface.

To match the MATLAB function mymagic.m, design an interface named Magic.

For example, the interface for the mymagic function:

function m = mymagic(in)
    m = magic(in);

might look like this:

 public interface Magic 
          double[,] mymagic(int in1);

Note the following:

  • The .NET interface has the same number of inputs and outputs as the MATLAB function.

  • You are deploying one MATLAB function, therefore you define one corresponding .NET method in your C# code.

  • Both MATLAB function and .NET interface process the same types: input type int and the output type two-dimensional double.

  • You specify the name of your deployable archive (magic, which resides in your auto_deploy folder) in your URL, when you call CreateProxy ("http://localhost:9910/magic").

Write, Build, and Run the .NET Application

Create a C# interface named Magic in Microsoft Visual Studio by doing the following:

  1. Open the Microsoft Visual Studio project, MagicSquare, that you created earlier.

  2. In Program.cs tab, paste in the code below.


    The URL value ("http://localhost:9910/mymagic_deployed") used to create the proxy contains three parts:

    • the server address (localhost).

    • the port number (9910).

    • the archive name (mymagic_deployed)

    using System;
    using System.Net;
    using MathWorks.MATLAB.ProductionServer.Client;
    namespace Magic 
    	 public class MagicClass 
        public interface Magic 
        	double[,] mymagic(int in1);
        public static void Main(string[] args)
        	MWClient client = new MWHttpClient();
           	Magic me = client.CreateProxy<Magic>
                         (new Uri("http://localhost:9910/mymagic_deployed"));
             	double[,] result1 = me.mymagic(4);
           catch (MATLABException ex)
             Console.WriteLine("{0} MATLAB exception caught.", ex);
           catch (WebException ex)
             Console.WriteLine("{0} Web exception caught.", ex);
       public static void print(double[,] x) 
          int rank = x.Rank;
          int [] dims = new int[rank];
          for (int i = 0; i < rank; i++)
             dims[i] = x.GetLength(i);
          for (int j = 0; j < dims[0]; j++)
             for (int k = 0; k < dims[1]; k++)
                if (k < (dims[1] - 1))
  3. Build the application. Click Build > Build Solution.

  4. Run the application. Click Debug > Start Without Debugging. The program returns the following console output:


Was this topic helpful?