Calling a MATLAB function from C#
Asked Answered
L

3

15

I developed a MATLAB function, and I'm looking for a way to call that function from another C# application and pass some parameters to it and get the results in the C# program.

I heard that I can use Dynamic Data Exchange (DDE) or COM objects, but have can I do it?

Lectra answered 5/5, 2011 at 17:16 Comment(2)
Did you try reading about PInvoke attribute in C# documentation? It will give you an idea of calling COM objects from .NET World.Untimely
You can pass matlab function handles to C# delegates. See my similiar post: #31409124Pullet
Y
23

There is nice example in the MATLAB Central.

It shows three ways on how to communicate with MATLAB:

  1. COM
  2. MATLAB .NET Bulider
  3. MATLAB compiler

COM (I do not have any experience with it)

Cons: MATLAB is required to be installed on the target computer.

MATLAB .NET builder compiles your MATLAB code to the .NET assembly and you can use it directly.

Pros: MATLAB is not required to be installed on the target computer

Cons: It's expensive

MATLAB compiler compiles your MATLAB code into a C/C++ library or EXE file. You can use it through P/Invoke.

Pros: MATLAB is not required to be installed on the target computer

Cons: It's expensive, a lot of P/Invoke.

Yeargain answered 5/5, 2011 at 21:14 Comment(3)
you should point out that for the second and third options, the produced binaries require the MCR to be installed on the target machine (MCR is freely available)Defeasible
The link is now dead :-(Lectra
MATLAB .NET builder is expensive with respect to what ?Groningen
L
6

There is a third option: delegates. Starting MATLAB -> load .NET assembly -> execute .NET function with delegate handle to a MATLAB function.

Liger answered 5/5, 2011 at 21:17 Comment(0)
A
2

There is a great example on this site on setting up everything. You can use MATLAB .NET deployment tool.

You need to

  • Install MCR (Matlab Compiler Runtime).
  • Deploy your Matlab function to .NET Assembly using Matlab Deploy Tool. This will create a .dll file.
  • Add .dll reference inside your .NET project.
  • Add reference to MATLAB.NET.

The advantage of this method is that the target machine does not require MATLAB to be installed, but on the downside the execution is quite expensive.

Aerie answered 29/8, 2015 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.