00001
00002
00003
00004
00005
00006
00007
00008
00009
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
00027
00028
00029
00030
00031
00032
00033
00034
00035 if (!(ep = engOpen("\0"))) {
00036 fprintf(stderr, "\nCan't start MATLAB engine\n");
00037 return EXIT_FAILURE;
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 T = mxCreateDoubleMatrix(1, 10, mxREAL);
00051 memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
00052
00053
00054
00055 engPutVariable(ep, "T", T);
00056
00057
00058
00059
00060
00061 engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
00062
00063
00064
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
00073
00074
00075 printf("Hit return to continue\n\n");
00076 fgetc(stdin);
00077
00078
00079
00080 printf("Done for Part I.\n");
00081 mxDestroyArray(T);
00082 engEvalString(ep, "close;");
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 buffer[BUFSIZE] = '\0';
00101 engOutputBuffer(ep, buffer, BUFSIZE);
00102 while (result == NULL) {
00103 char str[BUFSIZE+1];
00104
00105
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
00117
00118 engEvalString(ep, str);
00119
00120
00121
00122
00123
00124 printf("%s", buffer+2);
00125
00126
00127
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
00139
00140 printf("Done!\n");
00141 mxDestroyArray(result);
00142 engClose(ep);
00143
00144 return EXIT_SUCCESS;
00145 }
00146
00147
00148
00149
00150
00151
00152