How to call MATLAB from C/C++ in Windows 10 with IDE, for example, Visual Studio?

조회 수: 61 (최근 30일)
I am unable to find any method of calling MATLAB R2021b from C in Windows with IDE, for example, Visual Studio 2019.
Please provide an example code as well.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2023년 5월 26일
==============================================================
For an overall guidance about how to call MATLAB in C/C++, please refer to the following documentations.
- Call MATLAB from C
- Call MATLAB from C++
===============================================================
Follow the guidance below to call MATLAB engine in C/C++ IDE in Windows Visual Studio.
1. First, you need to register MATLAB as a COM server. If you have multiple versions of MATLAB, make sure that you are working on the same version of MATLAB that you want to call from C/C++.
Run MATLAB with administrator privillege. Then type:
>> !matlab -regserver
You can close the MATLAB window that appears.
You can find more information about this in the documentation below.
- Register MATLAB as a COM server
Settings for Visual Studio was found in 
2. Go to My computer, right click and find "settings". You will open "System" window. Find "Advanced system settings" on the top left.
Go to Environmental Variables > System Variables. Edit "Path".
Add 
C:\Program Files\MATLAB\R2021b\bin\win64
This path is reference on where is your libeng.dll located. Important moment: it can require to restart VS or even restart computer.
3. Run Visual Studio, and make a project for C/C++, and open "Properties" of the project.
4. Open "Configuration Manager" and make sure that the active solution platform is set to "x64" if you run 64 bit MATLAB.
5. In Project Properties > VC ++ Directories > Library Directories, add the path
C:\Program Files\MATLAB\R2021b\bin\win64
6. You can use "engine.h" in order to run MATLAB in C/C++ IDE. You can find the file in the following path
You can get the full path in MATLAB with a command below.
>> fullfile(matlabroot,'extern','include')
You can add the path in Configuration Properties > C/C++ > General > Additional Include Directories.
7. You need to use Linker inputs "libeng.lib" and "libmx.lib". You can find the path for these Linker inputs with the command below.
>> fullfile(matlabroot,'extern','lib','win64','microsoft')
You can add the path in Configuration Properties > Linker > General > Additional Library Directories.
8. Also, add the *.lib files mentioned above in Configuration Properties > Linker > Input > Additional Dependencies.
Now you are ready to run MATLAB engine in C/C++. Below is an example C code. You can run the code and see a cosine graph is plotted after MATLAB gets open.
 
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
#include <conio.h>
#include <ctype.h>
#include "engine.h"
using namespace std;
int main() {
Engine* ep = engOpen(NULL);
double x[1000];
int i;
double t = 0;
const double dt = 0.001;
mxArray* x_array = mxCreateDoubleMatrix(1000, 1, mxREAL);
mxDouble* px = mxGetDoubles(x_array);
for (i = 0; i < 1000; i++) {
x[i] = cos(2 * M_PI * t); // To check numbers in C
t += dt;
px[i] = x[i]; // indirectly accessing to x_array's value with a pointer variable px
}
engPutVariable(ep, "x", x_array);
engEvalString(ep, "plot(x)");
_getch(); // To make cmd stop until pressing any keys.
engClose(ep);
return 0;
}
 

추가 답변 (1개)

Angelo Yeo
Angelo Yeo 2023년 5월 26일
For those who are interested in running a demo, you can get a Visual Studio project file here.

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by