Programming and Graphics

(Kiana) #1

296 Introduction to C++ Programming and Graphics


//--- Matlab session:

engEvalString(lva, "b");

cout << buffer; // transfer back the matlab response

//--- End the session:

engClose(lva);

return 0;
}

The output of the code is:


>>
b=
5

Two-dimensional graph


The following code contained in the filemplot2d.ccgenerates data and
prepares a two-dimensional plot usingMatlabgraphics functions:


#include <iostream>
#include "engine.h"
using namespace std;

int main()
{

//--- Define data:

const short sdata = 5;
double x[sdata] ={0.0, 0.1, 0.5, 0.8, 0.9};
double y[sdata] ={0.0, 0.2, 0.3, 0.4, 0.42};

//--- Start a Matlab session:

Engine * iams = engOpen("matlab -nojvm");

/*--- Reserve the array ‘‘hronos’’
Copy into memory
Transfer the data */

mxArray * hronos = mxCreateDoubleMatrix(1, sdata, mxREAL);
memcpy((void *)mxGetPr(hronos), (void *)x, sizeof(x));
Free download pdf