Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Matlab Coder with c#

I want to integrate MATLAB Coder output with a C# project in Visual Studio 2010. My main idea is:

  • Create a *.m script in Matlab
  • Make sure the script is compatible with Matlab Coder.
  • Generate a C++ shared library (DLL) with Matlab Coder
  • Integrate with C# using something like this:

    //Starts the model execution. May take several minutes
    public static class DllHelper
    {
        [DllImport(@"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "Run()")]
        public static extern int Run();
    }
    
  • Also, I would like to be able to stop the execution and retrieve some partial results. To do this, I was thinking in two methods: StopExecution and RetrievePartialResults

    [DllImport(@"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint =     "StopExecution ()")]
    public static extern int StopExecution ();
    
    [DllImport(@"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "RetrievePartialResults()")]
    public static extern MyResults RetrievePartialResults();
    

Is it possible to do? If no, is there any alternatives? If yes, where can I find more examples?

like image 295
guilhermecgs Avatar asked Dec 25 '22 19:12

guilhermecgs


2 Answers

I have no idea if your plan works, but MATLAB Builder NE might be an alternative. It directly outputs a .Net dll without those hard limitations to the m-code.

The disadvantage is, that MCR is required on the target machine.

like image 166
Daniel Avatar answered Dec 28 '22 09:12

Daniel


I've done both ways. Formerly, our project was using MATLAB Compiler, but we now switched to Coder, because that avoids the overhead of having to install the runtime (which btw often failed to start inside the process for no apparent reason).

We compile the coder output as an unmanaged C project with a C interface and use a C++/CLR project as a wrapper. This has the advantage that we don't need to manually specify the interface for P/Invoke as the compiler will directly read the header files. The C++/CLR assembly is the linked to the C# project where the code is going to be used. Be aware that this is kind of expensive, so try to avoid calling the matlab code in a tight loop and better move the whole loop into the library if that is possible.

Here's a snippet from the wrapper library (still uses old Managed C++ syntax, but that doesn't matter here)

bool CTurconConnect2::Init()
{
      // Call the exported function in the library. Imported using a header file. 
      turcon_initialize();
      // Call one of the matlab functions (in this case, the entry function is manually defined 
      // in the C library, to have a clean interface)
      SetParameters(36.0,400.0,20.0,30.0,15.0,40.0,110.0, 0.0, 100.0);
      return true;
}

bool CTurconConnect2::Exit()
{
      turcon_terminate();
      return true;
}
like image 30
PMF Avatar answered Dec 28 '22 10:12

PMF