This code is the process by which C + + calls the Matab engine, the purpose of the code is simple, create a vector array in C + +, and then unit the vector array. The purpose of writing this code is to learn some data interaction between C + + and MATLAB for future reference.
#include <iostream>
#include <cstdio>
#include <vector>
#include <math.h>
#include <time.h>
#include "Engine.h"
#include<Eigen/Dense>
using namespace Eigen;
using namespace STD;
vector<double> veca;
void Createa (int m );
int Main ()
{
Const int num = 10;
Createa ( num ); //Create original matrix Veca
Double * pa = new double[10];
for ( int i = 0; i < num; I+ +)
{
PA [i] = Veca[i];
}
Engine * m_engine; //matlab engine
m_engine = NULL; //Initialize MATLAB engine
if ((! m_engine &&! (m_engine = engopen(NULL)))) //Open the Amatlab engine and fail to exit
{
return -1;
}
engsetvisible (m_engine, 1); //Set the visibility of the Matlab window when it is called, A1 when it is visible?
//Transfer the data that needs to be calculated into Matlab
Mxarray *veca= Mxcreatedoublematrix(1, mxreal); //Create a real number of 10 rows and 1 columns, the group array type is unique to Matlab
memcpy (void *) mxgetpr(veca), (void *) PA, ten * sizeof(double )); //Transfer data from D in C + + to Matlab
engputvariable (m_engine, "Vec", Veca); //Assignment statement, VEC is the input parameter in MATLAB code Y,VECA is an incoming parameter of C + +
//buffer is used to receive debugging information, when the MATLAB code is wrong, you can output buffer to view the error message
Char Buffer [255];
Buffer [254] = '+ ';
Engoutputbuffer (m_engine, buffer, 255);
engevalstring (m_engine, "CD (' D:\\code\\testeigen\\testeigen\\mat_code ')"); //Open the folder where the MATLAB code is located, notice that the path is double- backslash
engevalstring (m_engine,"Normalv = Normalizevec (VEC);"); //This is a call statement in MATLAB, note that the. m file name of MATLAB is consistent with the name of the called function, otherwise the function to be called is not found
printf ("%s", buffer); //When the MATLAB code goes wrong, it is used to output debugging information
the results of MATLAB are then passed back to C + +
Mxarray * mvec = NULL; //Also declare a type of bursts in MATLAB
Mvec = enggetvariable(m_engine, "Normalv");
Double * Cvec= NULL; //Declare a pointer in C + +
Cvec= (double*)mxgetdata(mvec); //Assign data from Matlab to data in C + +
for ( int i = 0; i<num; I+ +)
{
cout<< cvec[i] << "";
}
Mxdestroyarray (Veca); //Destroy MATLAB array
Mxdestroyarray (Mvec);
return 0;
system ("Pause" );
}
void Createa (int m )
{
Srand (time (NULL));
for ( int i = 0; I < m; I+ +)
{
Veca. push_back (Rand()%4 + 1);
cout << veca[i]<<"";
}
cout <<endl<<"-----------------------"<<Endl;
}
Output Result:
Matlab code:
The% function is to realize the unit of a vector
function Normalv = Normalizevec (VEC)
Normalv = vec/sqrt (sum (vec.^2, 2));
End
When an error occurs, you see the following message,
Then know that the Normaozeve function is undefined, and that the call statement is wrong.
C + + calls Matlab instance