9.4 Transferring data to theMatlabdomain 295
where the literalmxTYPEcan bemxREALfor an array with real elements
ormxCOMPLEXfor an array with complex elements consisting of a real and
an imaginary part.
- Second, we evaluate themxArrayusing the command:
memcpy(mxGetPr(arrayname), xp, sizeof(x));
wherexpis a pointer to the C++ arrayx.
- Third, we transfer themxArrayinto theMatlab-workspace using an
engine function:
engPutVariable(ep, "name", arrayname);
whereepis the declared engine session pointer name, andnameis the
name of the array in theMatlabdomain.
The following code contained in the filemtrans.ccdefines a numerical
variable, introduces its pointer, transfers the variable in theMatlabdomain,
and prints the variable:
#include <iostream>
#include "engine.h"
using namespace std;
int main()
{
double a = 5.0;
double * ap = &a;
//--- Start a session:
Engine * lva = engOpen("matlab12 -nojvm -nosplash");
//--- Define a character buffer:
const int BUFSIZE=256;
char buffer[BUFSIZE];
engOutputBuffer(lva, buffer, BUFSIZE);
/*--- Reserve the array ‘‘mxa’’
Copy into memory
Transfer the data */
mxArray * mxa = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy(mxGetPr(mxa), ap, sizeof(a));
engPutVariable(lva, "b", mxa);