Friday, September 09, 2005

MATLAB Engine API

Details are in this website and it is for Matlab6 and VC++6


For Matlab7 R14 and VC++.net2003, we use:


C++-General:

C:\MATLAB7\extern\include


Link-General-Additional library directory:

C:\MATLAB7\extern\lib\win32\microsoft\msvc70


Input-Additional Dependency:

libeng.lib libmx.lib libmat.lib libut.lib mclcom.lib mclcommain.lib mclmcr.lib mclmcrrt.lib mclxlmain.lib


#include "mclmcr.h" insteald of "matlab.h" in MatlabEng.h

copy "libut.dll, libmx.dll" from "C:\MATLAB7\bin\win32" to working directory.


/*********** some codes to transfer data between C and Matlab **********/

mxArray *T = NULL;

double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };


//create matrix

T = mxCreateDoubleMatrix(1, 10, mxREAL);

memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));


//send matrix T to matlab

message("Send matrix T to matlab");

matlab.PutVariable("Tt", T);


//Evaluate matlab command

matlab.EvalString("D = .5.*(-9.8).*Tt.^2;");


mxArray *DD = NULL;

DD = mxCreateDoubleMatrix(1, 10, mxREAL);

double inMyP[1][10];

DD = matlab.GetVariable("D");


//memcpy des, source, length

memcpy((void *)inMyP,(void *)mxGetPr(DD), sizeof(inMyP));


//printf("D= %f\n", *DD[0][1]);

for (int i=0; i 10; i++)

printf("%f\n", inMyP[0][i]);


//Plot results

message("Plot(T, D)");

matlab.EvalString("figure;");

matlab.EvalString("plot(Tt,D);");

matlab.EvalString("title('Position vs. Time for a falling object');");

matlab.EvalString("xlabel('Time (seconds)');");

matlab.EvalString("ylabel('Position (meters)');");

mxDestroyArray(DD);


/******* some codes from other website ********/

myP = mxCreateDoubleMatrix(5, 6, mxREAL);

double inMyP[5][6];

...

...

memcpy((void *)mxGetPr(myP), (void *)inMyP, sizeof(inMyP));

message("Send matrix myP to matlab");

matlab.PutVariable("myP", myP);

matlab.EvalString("myP=myP.*2;"); //nothing happens, WHY??

myP=matlab.GetVariable("myP");

...

//write some code to prints matrix

for (int i=0; i 5; i++)

{

for (int j=0; j 6; j++)

printf("%f\t", inMyP[i][j]);


printf("\n");

}

mxDestroyArray(myP);


execute own matlab function?? Just copy your m files in work directory of MATLAB

Location: C:\Data\Examples\Matlab_Engine

No comments:

Post a Comment