core/matlab/examples/eng_mat/engwindemo.c

00001 /*
00002  *  engwindemo.c
00003  *
00004  *  This is a simple program that illustrates how to call the MATLAB
00005  *  Engine functions from a C program for windows
00006  *
00007  * Copyright 1984-2003 The MathWorks, Inc.
00008  */
00009 /* $Revision: 1.1 $ */
00010 #include <windows.h>
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include "engine.h"
00015 
00016 #define BUFSIZE 256
00017 
00018 static double Areal[6] = { 1, 2, 3, 4, 5, 6 };
00019 
00020 int PASCAL WinMain (HINSTANCE hInstance,
00021                     HINSTANCE hPrevInstance,
00022                     LPSTR     lpszCmdLine,
00023                     int       nCmdShow)
00024 
00025 {
00026     Engine *ep;
00027     mxArray *T = NULL, *a = NULL, *d = NULL;
00028     char buffer[BUFSIZE+1];
00029     double *Dreal, *Dimag;
00030     double time[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
00031 
00032     /*
00033      * Start the MATLAB engine 
00034      */
00035     if (!(ep = engOpen(NULL))) {
00036         MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine", 
00037             (LPSTR) "Engwindemo.c", MB_OK);
00038         exit(-1);
00039     }
00040 
00041     /*
00042      * PART I
00043      *
00044      * For the first half of this demonstration, we will send data
00045      * to MATLAB, analyze the data, and plot the result.
00046      */
00047 
00048     /* 
00049      * Create a variable from our data
00050      */
00051     T = mxCreateDoubleMatrix(1, 10, mxREAL);
00052     memcpy((char *) mxGetPr(T), (char *) time, 10*sizeof(double));
00053 
00054     /*
00055      * Place the variable T into the MATLAB workspace
00056      */
00057     engPutVariable(ep, "T", T);
00058 
00059     /*
00060      * Evaluate a function of time, distance = (1/2)g.*t.^2
00061      * (g is the acceleration due to gravity)
00062      */
00063     engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
00064 
00065     /*
00066      * Plot the result
00067      */
00068     engEvalString(ep, "plot(T,D);");
00069     engEvalString(ep, "title('Position vs. Time for a falling object');");
00070     engEvalString(ep, "xlabel('Time (seconds)');");
00071     engEvalString(ep, "ylabel('Position (meters)');");
00072 
00073     /*
00074      * PART II
00075      *
00076      * For the second half of this demonstration, we will create another mxArray
00077      * put it into MATLAB and calculate its eigen values 
00078      * 
00079      */
00080       
00081      a = mxCreateDoubleMatrix(3, 2, mxREAL);         
00082      memcpy((char *) mxGetPr(a), (char *) Areal, 6*sizeof(double));
00083      engPutVariable(ep, "A", a); 
00084 
00085      /*
00086      * Calculate the eigen value
00087      */
00088      engEvalString(ep, "d = eig(A*A')");
00089 
00090      /*
00091      * Use engOutputBuffer to capture MATLAB output. Ensure first that
00092      * the buffer is always NULL terminated.
00093      */
00094      buffer[BUFSIZE] = '\0';
00095      engOutputBuffer(ep, buffer, BUFSIZE);
00096 
00097      /*
00098      * the evaluate string returns the result into the
00099      * output buffer.
00100      */
00101      engEvalString(ep, "whos");
00102      MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);
00103     
00104      /*
00105      * Get the eigen value mxArray
00106      */
00107      d = engGetVariable(ep, "d");
00108      engClose(ep);
00109 
00110      if (d == NULL) {
00111             MessageBox ((HWND)NULL, (LPSTR)"Get Array Failed", (LPSTR)"Engwindemo.c", MB_OK);
00112         }
00113     else {      
00114         Dreal = mxGetPr(d);
00115         Dimag = mxGetPi(d);             
00116         if (Dimag)
00117             sprintf(buffer,"Eigenval 2: %g+%gi",Dreal[1],Dimag[1]);
00118         else
00119             sprintf(buffer,"Eigenval 2: %g",Dreal[1]);
00120         MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR)"Engwindemo.c", MB_OK);
00121         mxDestroyArray(d);
00122     } 
00123 
00124     /*
00125      * We're done! Free memory, close MATLAB engine and exit.
00126      */
00127     mxDestroyArray(T);
00128     mxDestroyArray(a);
00129     
00130     return(0);
00131 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy