Implement a Type-Safe Interface

Implementing a type-safe interface usually requires the expertise of a .NET Developer because it requires performing a number of medium-to-advanced programming tasks.


Data objects that merely pass through either the target or MATLAB® environments may not need to be marshaled, particularly if they do not cross a process boundary. Because marshaling is costly, only marshal on demand.

After you write and test your MATLAB code, develop a .NET interface that supports the native types through the API in either C# or Visual Basic®. In this example, the interface, IAddOne, is written in C#.

Each method in the interface must exactly match a deployed MATLAB function.

The IAddOne interface specifies six overload of addOne:

using System.ServiceModel;

public interface IAddOne
    [OperationContract(Name = "addOne_1")]
    int addOne(int x);

    [OperationContract(Name = "addOne_2")]
    void addOne(ref int y, int x);

    [OperationContract(Name = "addOne_3")]
    void addOne(int x, ref int y);

    [OperationContract(Name = "addOne_4")]
    System.Double addOne(System.Double x);

    [OperationContract(Name = "addOne_5")]
    System.Double[] addOne(System.Double[] x);

    [OperationContract(Name = "addOne_6")]
    System.Double[][] addOne(System.Double[][] x);

As you can see, all methods have one input and one output (to match the MATLAB addOne function), though the type and position of these parameters varies.

Data Conversion Rules for Using the Type-Safe Interface

  • In a MATLAB function, declaration outputs appear before inputs. For example, in the addOne function, the output y appears before the input x. This ordering is not required for .NET interface functions. Inputs may appear before or after outputs or the two may be mixed together.

  • MATLAB Compiler SDK™ matches .NET interface functions to public MATLAB functions by function name and argument count. In the addOne example, both the .NET interface function and the MATLAB function must be named addOne and both functions must have an equal number of arguments defined.

  • The number and relative order of input and output arguments is critical.

    • In evaluating parameter order, only the order of like parameters (inputs or outputs) is considered, regardless of where they appear in the parameter list.

    • A function in the interface may have fewer inputs than its corresponding MATLAB function, but not more.

  • Argument mapping occurs according to argument order rather than argument name.

  • The function return value, if specified, counts as the first output.

  • You must use out parameters for multiple outputs.

    • Alternately, the ref parameter can be used for out. ref and out parameters are synonymous.

  • MATLAB does not support overloading of functions. Thus, all user-supplied overloads of a function with a given name will map to a function generated by MATLAB Compiler SDK.

See .NET Types to MATLAB Types for complete guidelines in managing data conversion with type-safe interfaces.