core/matlab/examples/eng_mat/engdemo.c

00001 /* $Revision: 1.1 $ */
00002 /*
00003  *  engdemo.c
00004  *
00005  *  This is a simple program that illustrates how to call the MATLAB
00006  *  Engine functions from a C program.
00007  *
00008  * Copyright 1984-2003 The MathWorks, Inc.
00009  * All rights reserved
00010  */
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include "engine.h"
00015 #define  BUFSIZE 256
00016 
00017 int main()
00018 
00019 {
00020     Engine *ep;
00021     mxArray *T = NULL, *result = NULL;
00022     char buffer[BUFSIZE+1];
00023     double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
00024 
00025     /*
00026      * Start the MATLAB engine locally by executing the string
00027      * "matlab"
00028      *
00029      * To start the session on a remote host, use the name of
00030      * the host as the string rather than \0
00031      *
00032      * For more complicated cases, use any string with whitespace,
00033      * and that string will be executed literally to start MATLAB
00034      */
00035     if (!(ep = engOpen("\0"))) {
00036         fprintf(stderr, "\nCan't start MATLAB engine\n");
00037         return EXIT_FAILURE;
00038     }
00039 
00040     /*
00041      * PART I
00042      *
00043      * For the first half of this demonstration, we will send data
00044      * to MATLAB, analyze the data, and plot the result.
00045      */
00046 
00047     /* 
00048      * Create a variable for our data
00049      */
00050     T = mxCreateDoubleMatrix(1, 10, mxREAL);
00051     memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
00052     /*
00053      * Place the variable T into the MATLAB workspace
00054      */
00055     engPutVariable(ep, "T", T);
00056 
00057     /*
00058      * Evaluate a function of time, distance = (1/2)g.*t.^2
00059      * (g is the acceleration due to gravity)
00060      */
00061     engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
00062 
00063     /*
00064      * Plot the result
00065      */
00066     engEvalString(ep, "plot(T,D);");
00067     engEvalString(ep, "title('Position vs. Time for a falling object');");
00068     engEvalString(ep, "xlabel('Time (seconds)');");
00069     engEvalString(ep, "ylabel('Position (meters)');");
00070 
00071     /*
00072      * use fgetc() to make sure that we pause long enough to be
00073      * able to see the plot
00074      */
00075     printf("Hit return to continue\n\n");
00076     fgetc(stdin);
00077     /*
00078      * We're done for Part I! Free memory, close MATLAB engine.
00079      */
00080     printf("Done for Part I.\n");
00081     mxDestroyArray(T);
00082     engEvalString(ep, "close;");
00083 
00084 
00085     /*
00086      * PART II
00087      *
00088      * For the second half of this demonstration, we will request
00089      * a MATLAB string, which should define a variable X.  MATLAB
00090      * will evaluate the string and create the variable.  We
00091      * will then recover the variable, and determine its type.
00092      */
00093       
00094     /*
00095      * Use engOutputBuffer to capture MATLAB output, so we can
00096      * echo it back.  Ensure first that the buffer is always NULL
00097      * terminated.
00098      */
00099 
00100     buffer[BUFSIZE] = '\0';
00101     engOutputBuffer(ep, buffer, BUFSIZE);
00102     while (result == NULL) {
00103         char str[BUFSIZE+1];
00104         /*
00105          * Get a string input from the user
00106          */
00107         printf("Enter a MATLAB command to evaluate.  This command should\n");
00108         printf("create a variable X.  This program will then determine\n");
00109         printf("what kind of variable you created.\n");
00110         printf("For example: X = 1:5\n");
00111         printf(">> ");
00112 
00113         fgets(str, BUFSIZE, stdin);
00114       
00115         /*
00116          * Evaluate input with engEvalString
00117          */
00118         engEvalString(ep, str);
00119         
00120         /*
00121          * Echo the output from the command.  First two characters are
00122          * always the double prompt (>>).
00123          */
00124         printf("%s", buffer+2);
00125         
00126         /*
00127          * Get result of computation
00128          */
00129         printf("\nRetrieving X...\n");
00130         if ((result = engGetVariable(ep,"X")) == NULL)
00131           printf("Oops! You didn't create a variable X.\n\n");
00132         else {
00133         printf("X is class %s\t\n", mxGetClassName(result));
00134         }
00135     }
00136 
00137     /*
00138      * We're done! Free memory, close MATLAB engine and exit.
00139      */
00140     printf("Done!\n");
00141     mxDestroyArray(result);
00142     engClose(ep);
00143     
00144     return EXIT_SUCCESS;
00145 }
00146 
00147 
00148 
00149 
00150 
00151 
00152 

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