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.
Open Microsoft Visual Studio.
Click File > New > Project.
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.
Type the name of the project in the Name field
(Magic
, for example).
Click OK. Your Magic
source
shell is created, typically named Program.cs
, by
default.
Create a reference in your MainApp
code to
the MATLAB Production Server client run-time library. In Microsoft Visual
Studio®, perform the following steps:
In the Solution Explorer pane within Microsoft Visual
Studio (usually on the right side), select the name of
your project, Magic
, highlighting it.
Right-click Magic
and select Add
Reference.
In the Add Reference dialog box, select the Browse tab.
Browse to the MATLAB Production Server client runtime, installed
at
.
Select $MPS_INSTALL
\client\dotnetMathWorks.MATLAB.ProductionServer.Client.dll
.
Click OK. MathWorks.MATLAB.ProductionServer.Client.dll
is
now referenced by your Microsoft Visual Studio project.
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"
Create a C# interface named Magic
in Microsoft Visual Studio by
doing the following:
Open the Microsoft Visual Studio project, MagicSquare
,
that you created earlier.
In Program.cs
tab, paste in the
code below.
Note:
The URL value (
|
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(); try { Magic me = client.CreateProxy<Magic> (new Uri("http://localhost:9910/mymagic_deployed")); double[,] result1 = me.mymagic(4); print(result1); } catch (MATLABException ex) { Console.WriteLine("{0} MATLAB exception caught.", ex); Console.WriteLine(ex.StackTrace); } catch (WebException ex) { Console.WriteLine("{0} Web exception caught.", ex); Console.WriteLine(ex.StackTrace); } finally { client.Dispose(); } Console.ReadLine(); } 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++) { Console.Write(x[j,k]); if (k < (dims[1] - 1)) { Console.Write(","); } } Console.WriteLine(); } } } }
Build the application. Click Build > Build Solution.
Run the application. Click Debug > Start Without Debugging. The program returns the following console output:
16,2,3,13 5,11,10,8 9,7,6,12 4,14,15,1