简体   繁体   English

C语言中的Matlab引擎函数返回零

[英]Matlab engine function in C returns zero

I was wondering if anyone could help me understand the syntax of Matlab engine in C. I am a beginner in C and am trying to call a custom Matlab function in C using Matlab Engine. 我想知道是否有人可以帮助我理解C语言中Matlab引擎的语法。我是C语言的初学者,正在尝试使用Matlab Engine调用C语言中的自定义Matlab函数。 Research that I have done includes reading the documentation for Matlab API, watching the lecture video from Mathworks, researching on Stack Overflow, reading the example eng.c file in Matlab, and Google. 我所做的研究包括阅读Matlab API文档,观看Mathworks的讲座视频,研究Stack Overflow,阅读Matlab中的示例eng.c文件和Google。

I have come up with this bit of code that compiles but the output returns zero. 我想出了一些可编译的代码,但是输出返回零。 The input array also doesn't return an array but an int. 输入数组也不返回数组,而是一个int。 I can't find a comprehensive walk through video of how to construct a C script that 我找不到完整的视频教程,以了解如何构建一个C脚本,

  1. takes in a vector 接受一个向量
  2. feeds it into a Matlab function and 将其输入到Matlab函数中
  3. returns output. 返回输出。

The docs explain making an array very clearly, but I can't find information that walks through in detail reading the array into Matlab, then getting output. 该文档解释了如何非常清楚地创建数组,但是我找不到详细了解将数组读入Matlab然后获取输出的信息。

Below, please see the code including comments for what I understand each section of code is doing. 在下面,请查看包含注释的代码,以了解我对代码各部分所做的理解。 The example function add_up just calls the sum function on an array. 示例函数add_up仅在数组上调用sum函数。 Any help would be great as I am stuck on why this is returning zero. 任何帮助都会很棒,因为我一直坚持为什么它返回零。

#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("%d\n",outp);

    printf("Done!\n");
    mxDestroyArray(pVarNum);

    engClose(ep);

    return EXIT_SUCCESS;

}

The engGetVariable function returns an mxArray* , not a double* : engGetVariable函数返回一个engGetVariable mxArray* ,而不是double*

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

This should be: 应该是:

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

Note also that the %d formatter in printf prints an int, you need to use %f for a double. 还要注意, printf中的%d格式化程序会打印一个int,您需要将%f用作double。

The problem was that engputVariable was returning 1, so Matlab engine wasn't taking in the array. 问题在于engputVariable返回1,因此Matlab引擎没有采用该数组。 I ended up copying and pasting the exact top segment of code from the Matlab engdemo.c (up to the call for engPutVariable ), then continuing with my code. 我最终从Matlab engdemo.c(直到对engPutVariable的调用)中复制并粘贴了最上面的代码,然后继续执行我的代码。 Encoding was in ASCII for the working code file. 工作代码文件采用ASCII编码。 I think the crucial piece is the initial call to open Matlab with a Null string, although I placed this exact piece of code in the non-working script and it did not lead to engPutVariable working. 我认为关键部分是使用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 engine\n");
        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("%f\n", out[ii]);
    }
    printf("%f\n",out[2]);

    printf("Done!\n");
    mxDestroyArray(T);
    mxDestroyArray(outp);
    engClose(ep);

    return EXIT_SUCCESS;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM