Search code examples
cmatlabmatlab-engine

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. 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.

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. I can't find a comprehensive walk through video of how to construct a C script that

  1. takes in a vector
  2. feeds it into a Matlab function and
  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.

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. 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;

}

Solution

  • The engGetVariable function returns an mxArray*, not a 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.