1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| // très important avec le #include <float.h> !!
_control87(MCW_EM, MCW_EM);
Engine *ep = NULL;
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 };
if (!(ep = engOpen(NULL)))
ShowMessage("Nok!");
else
ShowMessage("Ok!");
/*
* 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); |
Partager