9.3 TheMatlabengine functions 293
The following C++ code contained in the filemexec.ccasks forMat-
labcommands, which are then processed byMatlab. The result is put in a
memory buffer and displayed on the screen, and the session terminates when a
zero (0) is entered instead of a command:
#include <iostream>
#include "engine.h"
using namespace std;int main()
{
//--- Start a matlab engine session:Engine * skilaki;
skilaki = engOpen("matlab -nodesktop -nosplash");//--- Define a character buffer:const int BUFSIZE=256;
char buffer[BUFSIZE];
engOutputBuffer(skilaki, buffer, BUFSIZE);/*--- Define a character array to host a matlab command:
Initialize the first character to 1 */char matcom[50];
matcom[0]=1;
cout << "Please enter matlab commands: 0 to quit:"<< endl;//--- Keep asking for commands until 0 is entered:while(matcom[0]!= ’0’)
{
cin >> matcom;//--- Transfer the command to the matlab workspace:
engEvalString(skilaki, matcom);//--- Display the matlab response:
cout << buffer;
}//--- End the session:engClose(skilaki);return 0;
}