Monday, March 30, 2009

How to call Matlab in C#

I have had some difficulty in doing this, mostly due to Matlab's out of data documentation. Here is the low down.
  1. In C# add a reference by navigating to Project->Add Reference, then select the COM tab, then select the Matlab Application... At first there was no Matlab entry. I think I fixed this by running "Matlab -regserver" from the command line. This should register Matlab as a COM server.
  2. Create a C# Matlab object with this code: private MLApp.MLAppClass matlab
  3. Execute matlab code via strings like this: matlab.Execute(string);
  4. Retrive data from matlab using the GetFullMatrix command, like this: matlab.GetFullMatrix("variable", "base", ref real, ref imag); where variable is the name of the variable, real is the real part of the variable, image is the imaginary part of the variable.
You might also want to check out this mathworks documentation.
By the way, if you are interested in C# you should check out my colleagues blog here.

If you liked this post, please check out one of the sponsoring websites via the Google Ads.


  1. Thanks A Lot. I guess that should work..

  2. You sir, Rock! Thanks a million - was looking for this solution of ages

  3. Thank you so much! I constantly had problems with the Matlab compiler because I am using Matlab x64. This worked great! I really can't thank you enough!

  4. hi sir,

    i don't know how to fixed this by running "Matlab -regserver" from the command line as you state in this blog???