1

I wrote a C program which opens the MATLAB using engOpen and evaluates some values in MATLAB. I successfully compiled that code by linking the required libraries. But, unfortunately while executing that binary i am getting error

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.

Stop Working

I showed that above picture.

I am using windows 8.1 Pro, 64-bit RAM and MATLAB 2017a.

But, i successfully run that program by generating a MEX binary of that code in MATLAB. And successfully executed that MEX file.

Is it possible to compile & run a c code which uses matlab functions without generating a MEX file?

/*
 *  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);
}

This is what i had wrote and compiled using gcc compiler. But while executing facing some problems?

am.rez
  • 876
  • 2
  • 12
  • 24
  • As far as I could find (slightly over a year ago), this was not possible, although it was if you do it the other way around: write MatLab code, that calls c/c++ functions. You can also use return values straight from the MatLab command line: http://stackoverflow.com/questions/10085184/how-to-get-the-return-value-from-matlab-in-bash-script – DrDonut Apr 21 '17 at 11:49
  • Of course it's possible, that's why there is an Engine API, which you've used. You have made an error in your code. You need to debug your program to figure out where the error is. – Peter Apr 21 '17 at 12:57
  • What function(s) are you calling? What arguments are you passing to them? One candidate for this error is messing up memory. – CristiFati Apr 21 '17 at 16:29
  • You need a [http://stackoverflow.com/help/mcve](MCVE). The process of creating this will most likely narrow down the issue. Start with nothing but initializing the Engine, then add code back until it breaks. Then make the breaking code as simple as possible. If you haven't solved your problem yet, post that. – Peter Apr 24 '17 at 14:24
  • Why are you casting `mxGetPr(..)` to `(char *)`, when they are clearly documented as `(double *)` ? – Phil Goddard Apr 24 '17 at 17:06

0 Answers0