Sorry if this question has already been asked, but I couldn't find quite what I've been looking for. I was wondering if there is a way to work with a MATLAB compiled c-shared library in C# without using MCR/MATLAB (or in general any additional installation), e.g. to use this simple function test.m, compile it via mcc into a .dll and then use that in C#:
[x,y,z]=test(a,b,c)
%x,a are integers
%y,z,b are matrices
%c is a string (which e.g. could be used to switch between several modes)
x=a*a;
y=b*b-b;
z=y*y;
I'm not entirely sure if it's possible (let alone easy) to do this, but even using this simple example I can't get it to work in C#, though this may also have to do with my very limited C# experience. I suppose in this example I would have to use IntPtr to deal with the matrices, i.e. something like:
[DllImport("test.dll",EntryPoint="mlfTest")]
public static extern void testfunction([In] numargout, ref IntPtr x, ref IntPtr y, ref IntPtr z, [In] IntPtr a, [In] ref double[,] b, [In] ref c)
and then later try to get the data out of x,y,z via working around a bit with Marshal ? I suppose I'm also making some rather obvious mistakes, e.g. I'm not sure I can just pass ref double[,] to the dll and expect it to work.
I suppose if absolutely necessary I could use MCR (which would make things a lot easier), but right now I'm constrained.
Thanks in advance for your help.
Unfortunately, what you are asking for is impossible. Anything generated by the MATLAB Compiler needs the MATLAB Runtime installed on the system. Either as part of the MATLAB installation itself, or separately. Why? Because the code generated calls into those libraries.
You are aware that you can distribute the MCR installer for free without any royalty issues, right?
Now, on the other hand if you truly want to be free of MATLAB then you need to look at MATLAB Coder. It's the only component (that I know of) from MATLAB that lets you create true standalone code. The bad news, is that it only outputs to C/C++.
As others have stated, anything generated with the MATLAB Compiler requires the MCR.
Another option to integrate MATLAB and C# is using MATLAB COM Automation. See here for an example:
using System;
namespace testMATLAB
{
class Program
{
static void Main(string[] args)
{
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
System.Array pr = new double[4];
pr.SetValue(11, 0);
pr.SetValue(12, 1);
pr.SetValue(13, 2);
pr.SetValue(14, 3);
System.Array pi = new double[4];
pi.SetValue(1, 0);
pi.SetValue(2, 1);
pi.SetValue(3, 2);
pi.SetValue(4, 3);
matlab.PutFullMatrix("a", "base", pr, pi);
System.Array prresult = new double[4];
System.Array piresult = new double[4];
matlab.GetFullMatrix("a", "base", ref prresult, ref piresult);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With