C++ 是否可以运行';C';二进制里面调用matlab函数?
我编写了一个C++ 是否可以运行';C';二进制里面调用matlab函数?,c++,c,matlab,mex,engopen,C++,C,Matlab,Mex,Engopen,我编写了一个C程序,它使用engOpen打开MATLAB,并在MATLAB中计算一些值。通过链接所需的库,我成功地编译了该代码。但是,不幸的是,在执行二进制文件时,我遇到了错误 testEngOpen.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if solution is availa
C
程序,它使用engOpen
打开MATLAB,并在MATLAB中计算一些值。通过链接所需的库,我成功地编译了该代码。但是,不幸的是,在执行二进制文件时,我遇到了错误
testEngOpen.exe has stopped working.
A problem caused the program to stop working correctly. Windows will close the program and notify you if solution is available.
我展示了上面的图片
我使用的是windows 8.1 Pro、64位RAM和MATLAB 2017a
但是,通过在MATLAB中生成该代码的MEX二进制文件,我成功地运行了该程序。并成功执行了该MEX文件
在不生成MEX文件的情况下,是否可以编译并运行使用matlab函数的c代码
/*
* engwindemo.c
*
* This is a simple program that illustrates how to call the MATLAB
* Engine functions from a C program for windows
*
* Note:
* Use the Lcc or Microsoft Visual C++ compiler to build engwindemo.exe.
* The source code in engwindemo.c is not supported for other compilers.
*
* Copyright 1984-2003 The MathWorks, Inc.
*/
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
static double Areal[6] = { 1, 2, 3, 4, 5, 6 };
int PASCAL WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow)
{
Engine *ep;
mxArray *T = NULL, *a = NULL, *d = NULL;
char buffer[BUFSIZE+1];
double *Dreal, *Dimag;
double time[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/*
* Start the MATLAB engine
*/
if (!(ep = engOpen(NULL))) {
MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",
(LPSTR) "Engwindemo.c", MB_OK);
exit(-1);
}
/*
* PART I
*
* For the first half of this demonstration, we will send data
* to MATLAB, analyze the data, and plot the result.
*/
/*
* Create a variable from our data
*/
T = mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((char *) mxGetPr(T), (char *) time, 10*sizeof(double));
/*
* Place the variable T into the MATLAB workspace
*/
engPutVariable(ep, "T", T);
/*
* Evaluate a function of time, distance = (1/2)g.*t.^2
* (g is the acceleration due to gravity)
*/
engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
/*
* Plot the result
*/
engEvalString(ep, "plot(T,D);");
engEvalString(ep, "title('Position vs. Time for a falling object');");
engEvalString(ep, "xlabel('Time (seconds)');");
engEvalString(ep, "ylabel('Position (meters)');");
/*
* PART II
*
* For the second half of this demonstration, we will create another mxArray
* put it into MATLAB and calculate its eigen values
*
*/
a = mxCreateDoubleMatrix(3, 2, mxREAL);
memcpy((char *) mxGetPr(a), (char *) Areal, 6*sizeof(double));
engPutVariable(ep, "A", a);
/*
* Calculate the eigen value
*/
engEvalString(ep, "d = eig(A*A')");
/*
* Use engOutputBuffer to capture MATLAB output. Ensure first that
* the buffer is always NULL terminated.
*/
buffer[BUFSIZE] = '\0';
engOutputBuffer(ep, buffer, BUFSIZE);
/*
* the evaluate string returns the result into the
* output buffer.
*/
engEvalString(ep, "whos");
MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);
/*
* Get the eigen value mxArray
*/
d = engGetVariable(ep, "d");
engClose(ep);
if (d == NULL) {
MessageBox ((HWND)NULL, (LPSTR)"Get Array Failed", (LPSTR)"Engwindemo.c", MB_OK);
}
else {
Dreal = mxGetPr(d);
Dimag = mxGetPi(d);
if (Dimag)
sprintf(buffer,"Eigenval 2: %g+%gi",Dreal[1],Dimag[1]);
else
sprintf(buffer,"Eigenval 2: %g",Dreal[1]);
MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR)"Engwindemo.c", MB_OK);
mxDestroyArray(d);
}
/*
* We're done! Free memory, close MATLAB engine and exit.
*/
mxDestroyArray(T);
mxDestroyArray(a);
return(0);
}
/*
*engwindemo.c
*
*这是一个简单的程序,说明了如何调用MATLAB
*windows C程序中的引擎功能
*
*注:
*使用LCC或微软Visual C++编译器构建EngEnMel.EXE。
*其他编译器不支持engwindemo.c中的源代码。
*
*版权所有1984-2003 MathWorks公司。
*/
#包括
#包括
#包括
#包括
#包括“发动机.h”
#定义bufsize256
静态双区域[6]={1,2,3,4,5,6};
int PASCAL WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpszCmdLine,
国际展览(nCmdShow)
{
发动机*ep;
mxArray*T=NULL,*a=NULL,*d=NULL;
字符缓冲区[BUFSIZE+1];
双*Dreal,*Dimag;
双时间[10]={0,1,2,3,4,5,6,7,8,9};
/*
*启动MATLAB引擎
*/
如果(!(ep=engOpen(NULL))){
MessageBox((HWND)NULL,(LPSTR)“无法启动MATLAB引擎”,
(LPSTR)“Engwindemo.c”,MB_OK);
出口(-1);
}
/*
*第一部分
*
*在本演示的前半部分,我们将发送数据
*利用MATLAB对数据进行分析,并绘制结果图。
*/
/*
*从我们的数据创建一个变量
*/
T=mxCreateDoubleMatrix(1,10,mxREAL);
memcpy((char*)mxGetPr(T),(char*)时间,10*sizeof(double));
/*
*将变量T放入MATLAB工作区
*/
变量(ep,“T”,T);
/*
*计算时间的函数,距离=(1/2)g.*t.^2
*(g是重力引起的加速度)
*/
Engelastring(ep,“D=0.5.*(-9.8)。*T.^2;”);
/*
*绘制结果
*/
Engelastring(ep,“图(T,D);”);
Engelastring(ep,“标题(‘坠落物体的位置与时间’);”;
Engelastring(ep,“xlabel('时间(秒);”);
Engelastring(ep,“ylabel(‘位置(米)’);”);
/*
*第二部分
*
*在本演示的后半部分,我们将创建另一个mxArray
*将其放入MATLAB中,计算其特征值
*
*/
a=mxCreateDoubleMatrix(3,2,mxREAL);
memcpy((char*)mxGetPr(a),(char*)面积,6*sizeof(double));
engPutVariable(ep,“A”,A);
/*
*计算本征值
*/
Engelastring(ep,“d=eig(A*A')”);
/*
*使用engOutputBuffer捕获MATLAB输出。首先确保
*缓冲区始终以NULL结尾。
*/
缓冲区[BUFSIZE]='\0';
engOutputBuffer(ep、buffer、BUFSIZE);
/*
*求值字符串将结果返回到
*输出缓冲区。
*/
Engelastring(ep,“whos”);
MessageBox((HWND)NULL,(LPSTR)buffer,(LPSTR)“MATLAB-whos”,MB_OK);
/*
*获取数组的特征值
*/
d=工程变量(ep,“d”);
engClose(ep);
如果(d==NULL){
MessageBox((HWND)NULL,(LPSTR)“获取数组失败”,(LPSTR)“Engwindemo.c”,MB_OK);
}
否则{
Dreal=mxGetPr(d);
Dimag=mxGetPi(d);
如果(Dimag)
sprintf(缓冲区,“特征值2:%g+%gi”,Dreal[1],Dimag[1]);
其他的
sprintf(缓冲区,“特征值2:%g”,Dreal[1]);
MessageBox((HWND)NULL,(LPSTR)缓冲区,(LPSTR)“Engwindemo.c”,MB_OK);
MX阵列(d);
}
/*
*完成了!释放内存,关闭MATLAB引擎并退出。
*/
MX阵列(T);
MX阵列(a);
返回(0);
}
这是我使用gcc编译器编写和编译的。但在执行过程中遇到了一些问题?据我所知(一年多以前),这是不可能的,尽管如果你用另一种方法:编写调用c/c++函数的MatLab代码。您还可以直接从MatLab命令行使用返回值:当然,这是可能的,这就是为什么您使用了引擎API。您的代码中有一个错误。您需要调试程序以找出错误所在。您正在调用什么函数?你在向他们传递什么论据?此错误的一个可能原因是内存混乱。您需要[.创建此项的过程很可能会缩小问题的范围。只需初始化引擎,然后重新添加代码,直到它崩溃。然后使崩溃的代码尽可能简单。如果您还没有解决问题,请发布。为什么要将
mxGetPr(…)
转换为(char*)
,当它们被清楚地记录为(双*)
?据我所知(一年多以前),这是不可能的,但如果你用另一种方法:编写调用c/c++函数的MatLab代码。你也可以直接从MatLab命令行使用返回值:当然,这是可能的,这就是为什么你使用了引擎API。你在代码中犯了错误。你需要将程序调试到figu我们将找出错误所在。您正在调用什么函数?您将向它们传递什么参数?此错误的一个候选原因是内存混乱。您需要[.创建此引擎的过程很可能会缩小问题的范围。只需初始化引擎,然后重新添加代码,直到它崩溃。然后使崩溃的代码尽可能简单