EDN Admin
Well-known member
Is there a simple way to compile a 32 bit app with Visual C++ 2010 or 2012 Express?
!Used Matlab and Windows is 64 bit. Also Visual is configured to compile 64 bit apps./* $Revision: 1.8.4.4 $ */
/*
* engdemo.c
*
* A simple program to illustrate how to call MATLAB
* Engine functions from a C program.
*
* Copyright 1984-2011 The MathWorks, Inc.
* All rights reserved
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
int main()
{
Engine *ep;
mxArray *T = NULL, *result = NULL;
char buffer[BUFSIZE+1];
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
/*
* Call engOpen with a NULL string. This starts a MATLAB process
* on the current host using the command "matlab".
*/
if (!(ep = engOpen(""))) {
fprintf(stderr, "nCant start MATLAB enginen");
return EXIT_FAILURE;
}
/*
* PART I
*
* For the first half of this demonstration, send data
* to MATLAB, analyze the data, and plot the result.
*/
/*
* Create a variable for the data
*/
T = mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
/*
* 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));");
/*
* use fgetc() to pause long enough to be
* able to see the plot
*/
printf("Hit return to continuenn");
fgetc(stdin);
/*
* Were done for Part I! Free memory, close MATLAB figure.
*/
printf("Done for Part I.n");
mxDestroyArray(T);
engEvalString(ep, "close;");
/*
* PART II
*
* For the second half of this demonstration, we will request
* a MATLAB string, which should define a variable X. MATLAB
* will evaluate the string and create the variable. We
* will then recover the variable, and determine its type.
*/
/*
* Use engOutputBuffer to capture MATLAB output, so we can
* echo it back. Ensure first that the buffer is always NULL
* terminated.
*/
buffer[BUFSIZE] = ;
engOutputBuffer(ep, buffer, BUFSIZE);
while (result == NULL) {
char str[BUFSIZE+1];
/*
* Get a string input from the user
*/
printf("Enter a MATLAB command to evaluate. This command shouldn");
printf("create a variable X. This program will then determinen");
printf("what kind of variable you created.n");
printf("For example: X = 1:5n");
printf( > ");
fgets(str, BUFSIZE, stdin);
/*
* Evaluate input with engEvalString
*/
engEvalString(ep, str);
/*
* Echo the output from the command.
*/
printf("%s", buffer);
/*
* Get result of computation
*/
printf("nRetrieving X...n");
if ((result = engGetVariable(ep,"X")) == NULL)
printf("Oops! You didnt create a variable X.nn");
else {
printf("X is class %stn", mxGetClassName(result));
}
}
/*
* Were done! Free memory, close MATLAB engine and exit.
*/
printf("Done!n");
mxDestroyArray(result);
engClose(ep);
return EXIT_SUCCESS;
}
View the full article