Programming and Graphics

(Kiana) #1

9.4 Transferring data to theMatlabdomain 299


//--- z data:

for (int i=0; i<=Nx; i++)
{
for (int j=0; j<=Ny; j++)
{
z[i][j]=cos(2*3.14159*(x[i]+y[j]))*exp(-3.0*x[i]);
}
}

//--- Start a Matlab session:

Engine * gataki = engOpen("matlab12 -nodesktop");
engOutputBuffer(gataki, buffer, BUFSIZE);

/*--- Reserve the vector ‘‘xx’’
Copy into memory
Transfer the data */

mxArray * xx = mxCreateDoubleMatrix(1, Nx+1, mxREAL);
memcpy((void *)mxGetPr(xx), (void *)x, sizeof(x));
engPutVariable(gataki, "xplot", xx);

/*--- Reserve the vector ‘‘yy’’
Copy into memory
Transfer the data */

mxArray * yy = mxCreateDoubleMatrix(1, Ny+1, mxREAL);
memcpy((void *)mxGetPr(yy), (void *)y, sizeof(y));
engPutVariable(gataki, "yplot", yy);

/*--- Reserve the matrix ‘‘zz’’
Copy into memory
Transfer the data */

mxArray * zz = mxCreateDoubleMatrix(Ny+1, Nx+1, mxREAL);
memcpy((void *)mxGetPr(zz), (void *)z, sizeof(z));
engPutVariable(gataki, "zplot", zz);

//--- Matlab session:

engEvalString(gataki, "mesh(xplot,yplot,zplot);");
engEvalString(gataki, "hold on;");
engEvalString(gataki, "xlabel(’x’,’fontsize’,15)");
engEvalString(gataki, "ylabel(’y’,’fontsize’,15)");
engEvalString(gataki, "zlabel(’z’,’fontsize’,15)");
engEvalString(gataki, "set(gca,’fontsize’,15)");
Free download pdf