14

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
  • 1
    Did you try reading about PInvoke attribute in C# documentation? It will give you an idea of calling COM objects from .NET World. – kanchirk May 05 '11 at 17:30
  • You can pass matlab function handles to C# delegates. See my similiar post: http://stackoverflow.com/questions/31408624/passing-matlab-methods-as-delegates-to-net-object – CodyF Aug 06 '15 at 14:50

3 Answers3

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.

GarethD
  • 340
  • 2
  • 14
Lukas Kabrt
  • 5,441
  • 4
  • 43
  • 58
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user287107
  • 9,286
  • 1
  • 31
  • 47
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.

Octane
  • 1,200
  • 11
  • 11