Interface Matlab Project with C++ Project

1 view (last 30 days)
Hello,
I'm a student in computer ingeneering . I have a simulated electric system coded in matlab , and a controler coded in C++ . ( SMART GRID SYSTEM ). I need a way for the controler to communicate with the matlab simulated environnement.
I have 3 ways to do so :
  1. Use Matlab Compiler to create a DLL/shared C++ library.
  2. Use Matlab Coder to translate the matlab code into C++ code.
  3. Use Matlab Engine to make matlab and the C++ program communicate with each other.
But i have some questions:
  • about (1.) Does matlab compiler compile Object-Oriented Matlab project ?
  • about (2.) Does matlab coder translate Object-Oriented Matlab project ?
  • about (3.) Is it possible to access all informations in matlab from C++ code (i.e. with the Matlab Engine)?
  • What are the limits of matlab Compiler and matlab Coder ?

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 26 Feb 2014
Edited: Jacob Halbrooks on 26 Feb 2014
Here are a couple more ways to consider interfacing MATLAB with C++:
  • Write MEX-files for your C++ code to create functions that can be called directly in MATLAB
  • Write C wrappers around your C++ code for use with MATLAB's LOADLIBRARY to load the library in MATLAB and make its functions accessible using CALLIB
Some of the approaches have limitations:
  • MATLAB Compiler can deploy most MATLAB code , including MATLAB Classes. However, it does not accept a class as a top-level wrapper. For your shared library, you'd need to write functions that instantiate the class and call methods, and then you'd bundle these functions as your library.
  • MATLAB Coder actually translates MATLAB code to C/C++ (unlike MATLAB Compiler, which bundles and encrypts it) and has stricter language limitations. MATLAB Coder allows MATLAB Classes but disallows other constructs you may be using such as cell arrays and try/catch. See here for supported and limited functionality.
  • MATLAB Engine works by running MATLAB as its own background process, and the C interface allows you to execute arbitrary commands in it and access the workspace
To help guide your choice, I'd suggest the following questions:
  • Is it preferred to use MATLAB or a C++ program as the driver? (If MATLAB, I'd suggest using MEX)
  • If you are using a C++ program as the driver, do you have access to installed MATLAB on the machines where it will be run? (If you have access, I'd suggest MATLAB Engine)

More Answers (1)

Gueniat Patrice
Gueniat Patrice on 27 Feb 2014
Hello,
Thank you for the fast and precise answer ! Yes, I should've been more precise with my questions. The C++ Program is the driver and the core of the project. Matlab only simulates the environnement around it.
So i think i'm gonna use MATLAB Engine. Because the matlab part is prety big and coded by others peers, so we dont want to touch at it. And if converting it using Coder or Compiler implies modifications in it, that's not the best solution.
But i need more precision about Matlab Engine to be sure :
  • Matlab Engine is a C library ( not C++ ) ?
  • Is it possible to launch scripts and Matlab projects from the C code (using the engine) ?
  • Can the C code acces informations stored in matlab ? Or communicate with it ?
Precisions : The Matlab code is the environnement and should be modified by the C code ( so get inputs). And when the environnemnt is modified and transforms, the C code should get the new state from it (output).
  1 Comment
Jacob Halbrooks
Jacob Halbrooks on 27 Feb 2014
Edited: Jacob Halbrooks on 27 Feb 2014
You can use MATLAB Engine from C++ and run scripts and projects as well as put data and get data into/from MATLAB.

Sign in to comment.

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!