필터 지우기
필터 지우기

Calling Matlab from C++

조회 수: 2 (최근 30일)
Artyom
Artyom 2013년 8월 3일
I have some data in 2d std::vector
for (int j=0; j<2; j++){
result.push_back(vector<double>());
for (int i=0; i<10; i++) result[j].push_back(i);
}
result is 2x10 matrix. I'd like to send result to matlab.
#include "engine.h"
#pragma comment( lib, "libmx.lib" )
#pragma comment( lib, "libmex.lib" )
#pragma comment( lib, "libeng.lib" )
int main(int argc, char** argv){
Engine *ep;
ep = engOpen(NULL);
int const SIZE=2;
int const SIZE2=10;
double x[SIZE][SIZE2];
mxArray *A=NULL;
A=mxCreateDoubleMatrix(SIZE2, SIZE, mxREAL);
memcpy((void *)mxGetPr(A), (void *)x, sizeof(double)*SIZE*SIZE2);
engPutVariable(ep, "Data", A);
engEvalString(ep, "newData = Something(Data)");
double *cresult;
mxArray *mresult;
mresult = engGetVariable(ep,"newData");
cresult = mxGetPr(mresult);
mxDestroyArray(A);
mxDestroyArray(mresult);
engClose(ep);
return 0;
}
I also add path to directories:
C:\Program Files\MATLAB\MATLAB Production Server\R2012
C:\Users\User\Documents\MATLAB
and libraries:
C:\Program Files\MATLAB\MATLAB Production Server\R2012b\extern\lib\win64\microsoft
But I have such error: " fatal error LNK1120: 8 unresolved externals ", when i try to build it.
  댓글 수: 2
Artyom
Artyom 2013년 8월 4일
Ok, I solved it. I forgot to change platform from x32 to x64 and run the code with administrator privileges.
Kaustubha Govind
Kaustubha Govind 2013년 8월 5일
Artyom: You might want to post this as an answer and "accept" it, so that it is clear that this issue is resolved. Thanks!

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

답변 (1개)

Wayne King
Wayne King 2013년 8월 3일
If you want the results in the MATLAB workspace, why not create a MEX file with your C++ code?
  댓글 수: 1
Artyom
Artyom 2013년 8월 3일
No the result will be in C++.

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

카테고리

Help CenterFile Exchange에서 Call C++ from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by