MATLAB Application Program Interface Reference | Help Desk |
engEvalString
Evaluate expression in string
#include "engine.h"
int engEvalString
(Engine *ep, const char *string);
ep
string
engEvalString
evaluates the expression contained in string
for the MATLAB engine session, ep
, previously started by engOpen
. It returns a nonzero value if the MATLAB session is no longer running, and zero otherwise.
On UNIX systems, 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
.
Under Windows on a PC, engEvalString
communicates with MATLAB via ActiveX.
Send a simple mxArray to the engine, compute its eigenvalues, get back the vector containing the eigenvalues, and print the second one:
For UNIX:
/* engtest1.c */ #include <stdio.h> #include "engine.h" static double Areal[6] = {1,2,3,4,5,6}; void main() { Engine *ep; Matrix *a, *d; double *Dreal, *Dimag; a = mxCreateDoubleMatrix(3,2,mxREAL); memcpy(mxGetPr(a),Areal,6*sizeof(double)); mxSetName(a,"A"); if (!(ep = engOpen("matlab4 "))) { fprintf(stderr,"\nCan't start MATLAB engine"); exit(-1); } engPutArray(ep,a); engEvalString(ep,"d = eig(A*A')"); d = engGetArray(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]); } mxFree(a); mxFree(d); }For Windows:
#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 = mxCreateDoubleMatrix(3, 2, mxREAL); memcpy((char *) mxGetPr(a),(char *) Areal, 6*sizeof(double)); mxSetName(a, "A"); if (!(ep = engOpen(NULL))) { MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine", (LPSTR) "Engtest1.c", MB_OK); } else { mxfree(a); return(TRUE); engPutArray(ep, a); engEvalString(ep, "d = eig(A*A')"); d = engGetArray(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); } mxFree(d); } mxFree(a); return (TRUE); }See
engdemo.c
in the eng_mat
subdirectory of the examples
directory for a sample program that illustrates how to call the MATLAB engine functions from a C program.