engdemo.c
Go to the documentation of this file.
00001 /* $Revision: 1.8.4.4 $ */
00002 /*
00003  *      engdemo.c
00004  *
00005  *      A simple program to illustrate how to call MATLAB
00006  *      Engine functions from a C program.
00007  *
00008  * Copyright 1984-2011 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          * Call engOpen with a NULL string. This starts a MATLAB process
00027      * on the current host using the command "matlab".
00028          */
00029         if (!(ep = engOpen(""))) {
00030                 fprintf(stderr, "\nCan't start MATLAB engine !!!\n");
00031                 return EXIT_FAILURE;
00032         }
00033 
00034         /*
00035          * PART I
00036          *
00037          * For the first half of this demonstration, send data
00038          * to MATLAB, analyze the data, and plot the result.
00039          */
00040 
00041         /*
00042          * Create a variable for the data
00043          */
00044         T = mxCreateDoubleMatrix(1, 10, mxREAL);
00045         memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
00046         /*
00047          * Place the variable T into the MATLAB workspace
00048          */
00049         engPutVariable(ep, "T", T);
00050 
00051         /*
00052          * Evaluate a function of time, distance = (1/2)g.*t.^2
00053          * (g is the acceleration due to gravity)
00054          */
00055         engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
00056 
00057         /*
00058          * Plot the result
00059          */
00060         engEvalString(ep, "plot(T,D);");
00061         engEvalString(ep, "title('Position vs. Time for a falling object');");
00062         engEvalString(ep, "xlabel('Time (seconds)');");
00063         engEvalString(ep, "ylabel('Position (meters)');");
00064 
00065         /*
00066          * use fgetc() to pause long enough to be
00067          * able to see the plot
00068          */
00069         printf("Hit return to continue\n\n");
00070         fgetc(stdin);
00071         /*
00072          * We're done for Part I! Free memory, close MATLAB figure.
00073          */
00074         printf("Done for Part I.\n");
00075         mxDestroyArray(T);
00076         engEvalString(ep, "close;");
00077 
00078 
00079         /*
00080          * PART II
00081          *
00082          * For the second half of this demonstration, we will request
00083          * a MATLAB string, which should define a variable X.  MATLAB
00084          * will evaluate the string and create the variable.  We
00085          * will then recover the variable, and determine its type.
00086          */
00087 
00088         /*
00089          * Use engOutputBuffer to capture MATLAB output, so we can
00090          * echo it back.  Ensure first that the buffer is always NULL
00091          * terminated.
00092          */
00093 
00094         buffer[BUFSIZE] = '\0';
00095         engOutputBuffer(ep, buffer, BUFSIZE);
00096         while (result == NULL) {
00097             char str[BUFSIZE+1];
00098             /*
00099              * Get a string input from the user
00100              */
00101             printf("Enter a MATLAB command to evaluate.  This command should\n");
00102             printf("create a variable X.  This program will then determine\n");
00103             printf("what kind of variable you created.\n");
00104             printf("For example: X = 1:5\n");
00105             printf(">> ");
00106 
00107             fgets(str, BUFSIZE, stdin);
00108 
00109             /*
00110              * Evaluate input with engEvalString
00111              */
00112             engEvalString(ep, str);
00113 
00114             /*
00115              * Echo the output from the command.
00116              */
00117             printf("%s", buffer);
00118 
00119             /*
00120              * Get result of computation
00121              */
00122             printf("\nRetrieving X...\n");
00123             if ((result = engGetVariable(ep,"X")) == NULL)
00124               printf("Oops! You didn't create a variable X.\n\n");
00125             else {
00126                 printf("X is class %s\t\n", mxGetClassName(result));
00127             }
00128         }
00129 
00130         /*
00131          * We're done! Free memory, close MATLAB engine and exit.
00132          */
00133         printf("Done!\n");
00134         mxDestroyArray(result);
00135         engClose(ep);
00136 
00137         return EXIT_SUCCESS;
00138 }
 All Classes Namespaces Files Functions Variables Typedefs Defines


openni_cam
Author(s): Alexander Shekhovtsov
autogenerated on Sun Dec 1 2013 17:19:20