# engEvalString

## Purpose

Execute statement in string.

## C Synopsis

    #include "engine.h"
    int engEvalString(ep,string)
            Engine *ep;
            char *string;

## Fortran Synopsis

    integer*4 function engEvalString(ep,string)
    integer*4 ep
    character*(*) string

## Arguments

`ep`
  engine pointer
`string`
  string to execute

## Description

`engEvalString` causes the MATLAB engine process `ep` to execute the statement or expression contained in `string`. It returns zero if successful, and a nonzero value if an error occurs.

On UNIX system `engEvalString` sends commands to MATLAB by writing down a pipe connected to MATLAB's stdin. Any output resulting from the command that ordinarily appears on the screen is read back from stdout into the buffer defined by `engOutputBuffer`.

On VMS system `engEvalString` sends commands to MATLAB by writing to an input mailbox.Any output resulting from the command that ordinarily appears on the screen is read back from an output mailbox into the buffer defined by `engOutputBuffer`.

Under MS-Windows on a PC, `engEvalString` communicates with MATLAB via DDE.

## Examples

Send a simple Matrix to the engine, compute its eigenvalues, get back the vector containing the eigenvalues, and print the second one:

For UNIX and VMS:

    /* engtest1.c */
    #include <stdio.h>
    #include "engine.h"
    static double Areal[6] = {1,2,3,4,5,6};
    main()
    {
      Engine *ep;
      Matrix *a, *d;
      double *Dreal, *Dimag;
      a = mxCreateFull(3,2,REAL);
      memcpy(mxGetPr(a),Areal,6*sizeof(double));
      mxSetName(a,"A");
      if (!(ep = engOpen("matlab4 "))) {
       fprintf(stderr,"\nCan't start MATLAB engine");
       exit(-1);
      }
      engPutMatrix(ep,a);
      engEvalString(ep,"d = eig(A*A')");
      d = engGetMatrix(ep,"d")
      engClose(ep);
      Dreal = mxGetPr(d);
      Dimag = mxGetPi(d);
      if (Dimag) {
       printf("Eigenval 2: %g+%gi",Dreal[1],Dimag[1]);
      } else {
       printf("Eigenval 2: %g\n",Dreal[1]);
      }
      mxFreeMatrix(a);
      mxFreeMatrix(d);
    }

The Fortran version is in `engtest1.f`.

For MS-Windows on a PC:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "engine.h"
    static double Areal[6] = { 1, 2, 3, 4, 5, 6 };
    int WINAPI WinMain (HANDLE hInstance,
                                                HANDLE hPrevInstance,
                                                LPSTR  lpszCmdLine,
                                                int    nCmdShow)
    {
            Engine *ep;
            Matrix *a, *d;
            double *Dreal, *Dimag;
            char  buff[256];
            a = mxCreateFull(3, 2, 0);
            memcpy((char *) mxGetPr(a),(char *) Areal, 6*sizeof(
    double));
            mxSetName(a, "A");
            engWinInit (hInstance);
            if (!(ep = engOpen(lpszCmdLine))) {
                    MessageBox ((HWND)NULL,
                    (LPSTR)"Can't start MATLAB engine",
                            (LPSTR) "Engtest1.c", MB_OK);
            } else {
                    mxfreeMatrix(a);
                    return(TRUE);
                    engPutMatrix(ep, a);
                    engEvalString(ep, "d = eig(A*A')");
                    d = engGetMatrix(ep, "d");
                    engClose(ep);

                    Dreal = mxGetPr(d);
                    Dimag = mxGetPi(d);

                    if (Dimag){
                    sprintf(buff,"Eigenval 2: %g+%gi",
                    Dreal[1],Dimag[1]);
                    } else {
                                  sprintf(buff,"Eigenval 2: %g",Dreal[1]);
                            MessageBox((HWND)NULL, (LPSTR)buff,
                            (LPSTR)"Engtest1.c", MB_OK);
                    }
                            mxFreeMatrix(d);
                    }
                    mxFreeMatrix(a);
                    return (TRUE);
    }

(c) Copyright 1994 by The MathWorks, Inc.