This demo shows three ways to integrate MATLAB code into a C# project. All three methods use the same example from MATLAB, but each interacts with it in a different fashion.
The first method shows how to use the MATLAB as an automation server from C# using the engine interface via com automation. This allows you to simultaneously debug your C# application from both the C# side and the MATLAB side, using debuggers on each side.
The second method uses MATLAB Builder for .NET to create a .NET assembly. This assembly exposes the MATLAB example as a method of a class included in the component. This method can be used directly in C#. It provides intellisense, automatic data marshalling and garbage collection. This .NET assembly can be deployed royalty-free to machines that do not have MATLAB.
The third method uses MATLAB Compiler to create a C shared library ? unmanaged code. This library exposes the MATLAB example ? but not in a manner that can be used directly in .NET. Instead a wrapper class was created to provide entry into the method included the library, and to marshal the data from the managed framework into the unmanaged library. This C Shared library can also be deployed royalty-free to machines that do not have MATLAB.
It should be noted that third example is very fragile. It is not the recommended method of C# and MATLAB integration. It has only been provided to emphasize the additional work which becomes necessary to integrate C# with MATLAB when you do not use Builder for .NET. This is not a general solution to integrating a MATLAB produced C shared library with .NET but a single purpose solution designed only to work for this example. Upon further inspection it should be clear that the lack of typing when moving from managed to unmanaged code dramatically increases the risk of data handling errors. The lack of intellisense, automatic data marshalling, and garbage collection are additional detractors from this method.