Interoperating between Matlab and C#
Asked Answered
T

7

20

After peeking around the internet it looks like it is possible to interop between C# and Matlab. I am wondering if anyone has had success with it and what they did to do so. If possible somehow pulling it off without the use of COM. Thanks for your time.

Trueblood answered 12/1, 2009 at 3:11 Comment(0)
N
10

Yes, quite possible. Though I ended up using the C interface and calling into that using a mixed-mode DLL (and getting C# to call into that... but that was because I was also interfacing with some other C code). It's quite straightforward. On computers where you want to run your program, you'll need to install Matlab Runtime MCRInstaller.exe.

edit: removed broken link

Nole answered 12/1, 2009 at 3:25 Comment(0)
S
20

Beginning with the R2009a release of MATLAB, .NET objects can be accessed from MATLAB:

http://www.mathworks.com/help/techdoc/matlab_external/brpb5k6.html

In older versions of MATLAB, it is possible to access .NET objects from MATLAB using CCW:

http://www.mathworks.com/support/solutions/data/1-5U8HND.html?solution=1-5U8HND

and the MATLAB engine from .NET:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f135590.html#f135616

You can also use the MATLAB Builder NE to wrap m-code into .NET assemblies.

http://www.mathworks.com/products/netbuilder/

Shew answered 12/1, 2009 at 14:55 Comment(2)
Out of all the options, this is both the last thing I tried, and what I ended up doing. From the Mathworks perspective, it's the most supported and documented scenario. Keep in mind that 32-bit or 64-bit versions of Matlab can only host 32-bit or 64-bit in-process COM servers, respectively.Annotation
The first link is broken.Farouche
N
10

Yes, quite possible. Though I ended up using the C interface and calling into that using a mixed-mode DLL (and getting C# to call into that... but that was because I was also interfacing with some other C code). It's quite straightforward. On computers where you want to run your program, you'll need to install Matlab Runtime MCRInstaller.exe.

edit: removed broken link

Nole answered 12/1, 2009 at 3:25 Comment(0)
V
3

Yes, I managed to do that and it was mostly painless.

You'd have to compile your MATLAB code for .NET (as explained in pervious answers), run MCR installer, reference MWArray.dll and the complied code dll in your .NET project and off you go.

The only annoyance was the casting (and sometimes multiple castings!) that the interface forces on you (using explicit in the cast operators)

Virgilvirgilia answered 12/1, 2009 at 15:10 Comment(0)
F
1

There is a free and open source .NET wrapper for Matlab, and it is very simple: http://www.codeproject.com/KB/dotnet/matlabeng.aspx

I use this.

Facia answered 28/7, 2009 at 18:29 Comment(0)
P
0

If you're willing to spend money this may be what you need:

http://www.mathworks.com/products/netbuilder/

It can generate wrappers for Matlab code that allow it to be called from both .NET and COM.

Petitionary answered 12/1, 2009 at 3:29 Comment(2)
Looks viable but I am a stingy stingy student.Trueblood
But be careful. No rapper worth his salt would like being called this way or that when he's in the middle of a song.Theological
S
0

In the R2009a pre-release you can call .net assemblies from MATLAB.

Salgado answered 21/1, 2009 at 20:34 Comment(0)
D
0

I'm not sure how much this applies to you but try looking into matlab executables or MEX Basically, it's really easy to write a program in C or FORTRAN, include a few libraries and functions, and then you can use you C or FORTRAN function in matlab

Dot answered 1/2, 2009 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.