C中的Matlab引擎函数返回零



我想知道是否有人能帮助我理解C中Matlab引擎的语法。我是C的初学者,正在尝试使用Matlab引擎调用C中的自定义Matlab函数。我所做的研究包括阅读Matlab API的文档,观看Mathworks的讲座视频,研究Stack Overflow,阅读Matlab中的示例eng.c文件,以及Google。

我已经想出了这段代码,它可以编译,但输出为零。输入数组也不返回数组,而是返回int。我找不到一个关于如何构建的C脚本的全面视频

  1. 接受向量
  2. 将其输入到Matlab函数中
  3. 返回输出

文档非常清楚地解释了如何制作数组,但我找不到详细的信息,无法将数组读取到Matlab中,然后获得输出。

下面,请查看代码,其中包括我对代码每一部分所做工作的理解。示例函数add_up只是调用数组上的sum函数。任何帮助都将是伟大的,因为我被困在为什么这是归零。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
int main() {
//Open call to matlab engine
Engine *ep;
//Use this conjunction with define BUFSIZe 256 to create double
//extData is a variable to read external data
//Number in brackets refer to size
//We are using double in this case and create the external data using initialization
double extData[10]={1.0,4.0,7.0,2.0,5.0,8.0,3.0,6.0,9.0,10.0};
//Next step is to make a pointer of type mxArray 
//These are pointers to an array of any size or type
mxArray *pVarNum;
double *outp;
//After we make a matrix for the double data initialized above
//Initialized to 0
pVarNum=mxCreateDoubleMatrix(1,10,mxREAL);
//Our array needs to be assigned a variable name for Matlab
//Workspace
//const char *myDouble = "T";
//Our matlab matrix is initialized to zero. We need to use
//The C memcpy function to get the data from extData to
//Get the array data using the pointer to pVarNum
//Use mxGetPr, a mxGet function
memcpy((void *)(mxGetPr(pVarNum)), (void *)extData,sizeof(extData));
//Place the variable T into the Matlab workspace
engPutVariable(ep,"T",pVarNum);
//Evalute test function
engEvalString(ep, "out=T+1");
//Make a pointer to the matlab variable
outp=engGetVariable(ep,"out");
//Now make a pointer to the C variable
printf("%dn",outp);
printf("Done!n");
mxDestroyArray(pVarNum);
engClose(ep);
return EXIT_SUCCESS;
}

engGetVariable函数返回mxArray*,而不是double*:

double *outp;
/* ... */
outp = engGetVariable(ep, "out");
printf("%dn", outp);

这应该是:

mxArray *out;
double *outp;
int ii;
/* ... */
out = engGetVariable(ep, "out");
outp = mxGetPr(out);
for (ii = 0; ii < 9; ii++) {
printf("%f, ", outp[ii]);
}
printf("%fn", outp[9]);

还要注意,printf中的%d格式化程序打印一个int,您需要使用%f作为double。

问题是engputVariable返回1,因此Matlab引擎没有接收数组。最后,我从Matlabengdemo.c中复制并粘贴了确切的顶部代码段(直到调用engPutVariable(,然后继续我的代码。工作代码文件的编码为ASCII。我认为关键部分是用Null字符串打开Matlab的初始调用,尽管我将这段代码放在了不工作的脚本中,但它并没有导致engPutVariable工作。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define  BUFSIZE 256
int main()
{
Engine *ep;
mxArray *T = NULL, *outp = NULL;
double *out;
int ii;
char buffer[BUFSIZE+1];
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

if (!(ep = engOpen(""))) {
fprintf(stderr, "nCan't start MATLAB enginen");
return EXIT_FAILURE;
}
T = mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
engPutVariable(ep, "T", T);

//Evalute test function. This is new
engEvalString(ep, "D=add_up(T);");
//Make a pointer to the matlab variable
if((outp=engGetVariable(ep,"D"))==NULL){
printf("Oops!");
}
out= mxGetPr(outp);
//Now make a pointer to the C variable
for (ii=0; ii<10; ii++) {
printf("%fn", out[ii]);
}
printf("%fn",out[2]);
printf("Done!n");
mxDestroyArray(T);
mxDestroyArray(outp);
engClose(ep);
return EXIT_SUCCESS;
}

最新更新