필터 지우기
필터 지우기

Interface Matlab Project with C++ Project

조회 수: 3 (최근 30일)
Gueniat Patrice
Gueniat Patrice 2014년 2월 26일
편집: Jacob Halbrooks 2014년 2월 27일
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 ?

채택된 답변

Jacob Halbrooks
Jacob Halbrooks 2014년 2월 26일
편집: Jacob Halbrooks 2014년 2월 26일
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)

추가 답변 (1개)

Gueniat Patrice
Gueniat Patrice 2014년 2월 27일
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
Jacob Halbrooks
Jacob Halbrooks 2014년 2월 27일
편집: Jacob Halbrooks 2014년 2월 27일
You can use MATLAB Engine from C++ and run scripts and projects as well as put data and get data into/from MATLAB.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by