MATLAB® data types are incompatible with native .NET types. To send data between your application and .NET, you perform these tasks:
Marshal data from .NET input data to a deployed function by creating an
MWArrayobject from native .NET data. The
publicfunctions in a deployed component return
Marshal the output MATLAB data in an
MWArrayinto native .NET data by calling one of the
MWArraymarshaling methods (
ToArray(), for example).
While you can marshal the data manually using
MWArray data types,
using a type-safe interface simplifies the process.
Manual Data Marshaling Without a Type-Safe Interface
Manually marshaling data adds complexity and potential failure points to the task of integrating deployed components into a .NET application. This is particularly true for these reasons:
Your application cannot detect type mismatch errors until run time. For example, you might accidentally create an
MWArrayfrom a string and pass the array to a deployed function that expects a number. Because the wrapper code generated by MATLAB Compiler SDK™ expects an
MWArray, the .NET compiler is unable to detect this error and the deployed function either throws an exception or returns the wrong answer.
Your end users must learn how to use the
MWArraydata type or alternately mask the
MWArraydata type behind a manually written (and manually maintained) API. This introduces unwanted training time and places resource demands on a potentially overcommitted staff.
Data Marshaling Without a Type-Safe Interface
You may find
MWArray methods more efficient when passing large data
values in loops to one or more deployed functions. In such cases, creating an
MWArray object allows you to marshal the data only once, whereas
type-safe interfaces marshal inputs on every call.
Simplified Data Marshaling with a Type-Safe Interface
You can avoid performing
MWArray data marshaling by using
type-safe interfaces. Such interfaces minimize explicit type
conversions by hiding the
MWArray type from the calling application.
Using type-safe interfaces allows .NET developers to work directly with familiar native
Data Marshaling With a Type-Safe Interface
Some of the reasons to implement type-safe interfaces include:
You avoid training and coding costs associated with teaching end users to work with the
You minimize cost of data you must marshal by either placing
MWArrayobjects in type-safe interfaces or by calling
MWArrayfunctions in the deployed MATLAB code.
Flexibility — you mix type-safe interfaces with manual data marshaling to accommodate data of varying sizes and access patterns. For example, you may have a few large data objects (images, for example) that would incur excess cost to your organization if managed with a type-safe interface. By mixing type-safe interfaces and manual marshaling, smaller data types can be managed automatically with the type-safe interface and your large data can be managed on an as-needed basis.
For an example on implementing type-safe interfaces, see Implement Type-Safe Interface and Integrate into .NET Application.
How Type-Safe Interfaces Work
Compiler SDK .NET assembly exports one or more public methods that accept and return data
MWArray objects. Adding a type-safe interface to a MATLAB
Compiler SDK assembly creates another set of methods (with the same names) that accept
and return native .NET types.
You may create multiple type-safe interface methods for a single MATLAB function. Type-safe interface methods follow the standard .NET methods for overloading.
The following figure illustrates the data paths between the .NET host application and the deployed MATLAB function through a type-safe interface.
The MATLAB function
addOne returns its input plus one. Deploying
addOne with a type-safe interface creates two .NET
One that accepts and returns .NET
One that accepts and returns
Notice that the type-safe methods co-exist with the
methods. Your .NET application may mix and match calls to either type of method, as