Main Content

Invoke MATLAB Functions Dynamically

To dynamically invoke MATLAB® functions, specify the function name as one of the parameters to the method invoking the request. You do not need to create a compiled interface that models the contents of a deployable archive, nor do you have to change your client application if there are changes to functions in the deployable archive.

To dynamically invoke a MATLAB function:

  1. Instantiate an MWClient instance.

  2. Create a reflection-based proxy object using one of the CreateComponentProxy() methods of the client connection.

  3. Invoke the function, or functions, using one of the Invoke() methods of the reflection-based proxy.

Create Reflection-Based Proxy

A reflection-based proxy implements the MWInvokable interface and provides methods that allow you to directly invoke any MATLAB function deployed as part of a deployable archive. As with the interface-based proxy, a reflection-based proxy is created from an MWClient object. The MWClient interface has two methods for creating a reflection-based proxy:

  • MWInvokable CreateComponentProxy(URL archiveURL) creates a proxy that uses standard MATLAB data types.

  • MWInvokable CreateComponentProxy(URL archiveURL, MWMarshallingRules marshallingRules) creates a proxy that uses structures.

To create a reflection-based proxy for invoking functions in the myMagic archive, hosted on your local computer:

MWClient myClient = new MWHttpClient();

Uri archiveURL = new Uri("http://localhost:9910/myMagic");
MWInvokable myProxy =  myClient.CreateComponentProxy(archiveURL);

Invoke MATLAB Function with Dynamic Proxy

A dynamic proxy has three methods for invoking functions on a server:

  • Object[] Invoke(string functionName, IList<Type> targetTypes, params Object[] inputs) invokes a function that returns multiple values.

  • T Invoke<T>(string functionName, params Object[] inputs) invokes a functions that returns a single value.

  • void Invoke(string functionName, params Object[] inputs) invokes a function that returns no values.

All of the methods map to the MATLAB function as follows:

  • First argument is the function name

  • Last arguments are the function inputs

Return Multiple Outputs

The MATLAB function myLimits returns two values.

function [myMin,myMax] = myLimits(myRange)
 myMin = min(myRange);
 myMax = max(myRange);

To invoke myLimits from a .NET client, use the Invoke() method that takes a list of target types:

double[] myRange = new double[]{2,5,7,100,0.5};
IList<Type> targetTypes = 
    new List<Type> { typeof(double), typeof(double) };
Object[] myLimits = myProxy.Invoke("myLimits", targetTypes, myRange);
double myMin = myLimits[0];
double myMax = myLimits[1];
Console.WriteLine("min: {0:f} max: {1:f}", myMin, myMax);

This form of Invoke() always returns Object[]. The contents of the returned array are typed based on the contents of targetType.

Return a Single Output

The MATLAB function addmatrix returns a single value.

function a = addmatrix(a1, a2)
a = a1 + a2;

To invoke addmatrix from a .NET client, use the Invoke() method that does not take the number of return arguments:

double[,] a1 = new double[,] {{1,2,3},{3,2,1}};
double[,] a2 = new double[,] {{4,5,6},{6,5,4}};
Object[] inputs = new Object[2];
inputs[0] = a1;
inputs[1] = a2;
double[,] result = myProxy.Invoke<double[,]>("addmatrix", inputs);

// display the result

Return No Outputs

The MATLAB function foo does not return a value.

function foo(a1)

To invoke foo from a .NET client, use the Invoke() method that returns void:

double[,] a = new double [,] {{1,2,3},{3,2,1}};
myProxy.Invoke("foo", a);

Create Custom Marshaling Rules

You need to provide custom marshaling rules to the reflection-based proxy if:

  • any MATLAB function in a deployable archive uses structures

  • any MATLAB in a deployable archive requires a custom setting to the default marshaling rules.

    There are default rules marshaling NaN, DateTime, .NET null, 1xN vectors, and Nx1 vectors.

To provide marshaling rules to the proxy:

  1. Implement a new set of marshaling rules by extending MWDefaultMarshalingRules to override the defaults.

  2. Create the proxy using CreateComponentProxy(URL archiveURL, MWMarshallingRules marshalingRules).

The deployable archive studentCheck includes functions that use a MATLAB structure of the form

S = 
name: 'Ed Plum'
score: 83
grade: 'B+'

Client code represents the MATLAB structure with a class named Student. To create a marshaling rule for dynamically invoking the functions in studentChecker, create a class named studentMarshaller.

class studentMarshaler : MWDefaultMarshalingRules
  public override IList<Type> StructTypes()
    get { return new List<Type> { typeof(Student) }; }

Create the dynamic proxy for studentCheck by passing studentMarshaller to createComponentProxy().

URL archiveURL = new URL("http://localhost:9910/studentCheck");
myProxy =  myClient.CreateComponentProxy(archiveURL,
                                         new StudentMarshaler());

For more information about using MATLAB structures, see Marshal MATLAB Structures (structs) in C#.

For more information about the other data marshaling rules, see Data Conversion with C# and MATLAB Types.

Related Topics