学习了一下c++中调用matlab函数的方法。之前一直用system command的形式,但是长时间读取一个动态的文本中的数据,似乎不太稳定,经常卡死在一个错误上"Invalid file identifier. Use fopen to generate a valid file identifier."。想试试用matlab eigen直接获取matlab函数返回值。步骤总结如下。
系统环境:Ubuntu 12.04, g++ 4.6, matlab 2012a
1)编写“example2.cpp”,从c++里传递数组值到matlab里面绘制正余弦曲线。代码如下:
/*
* example2.cpp
*
* This is a simple program that illustrates how to call the
* MATLAB engine functions from a C++ program.
*
* The program pass array from c++ to matlab, and let matlab plot the lines. *
*/
#include <iostream>
#include <math.h>
#include "engine.h"
#include<string.h>
#define PI 3.1415926
using namespace std;
int main() {
const int N = 100;
Engine *ep; //定义Matlab引擎指针。
if (!(ep=engOpen("\0"))) //测试是否启动Matlab引擎成功。
{
cout<< "Can't start MATLAB engine!"<<endl;
return EXIT_FAILURE;
}
double *X = new double [N];
double *Y = new double [N];
double *Z = new double [N];
// Generate uniform numbers between -2*PI and 2*PI.
for(int index = 0; index < N; index++) {
double step = 4*PI/N;
double x = step*index - 2*PI;
X[index] = x;
cout<<X[index]<<" ";
// Define Y as cosine values of x.
Y[index] = cos(x);
// Define Z as sine values of x.
Z[index] = sin(x);
}
//定义mxArray,为1行,N列的实数数组。
mxArray *xx = mxCreateDoubleMatrix(1, N, mxREAL);
mxArray *yy = mxCreateDoubleMatrix(1, N, mxREAL);
mxArray *zz = mxCreateDoubleMatrix(1, N, mxREAL);
//将c++数组中的值复制到对应的mxArray中
memcpy(mxGetPr(xx), X, N*sizeof(double));
memcpy(mxGetPr(yy), Y, N*sizeof(double));
memcpy(mxGetPr(zz), Z, N*sizeof(double));
//将mxArray数组写入到Matlab工作空间
engPutVariable(ep, "x", xx);
engPutVariable(ep, "y1", yy);
engPutVariable(ep, "y2", zz);
//向Matlab引擎发送画图命令。
engEvalString(ep, "plot(x, y1, '-.ms'); hold on; plot(x, y2, '-go');");
//销毁mxArray数组xx和yy。
mxDestroyArray(xx);
mxDestroyArray(yy);
//Use cin.get() to make sure that we pause long enough to be able to see the plot.
cout<<"Hit any key to exit!"<<endl;
cin.get();
//关闭Matlab引擎。
engClose(ep);
//释放指针内存
delete X;
delete Y;
delete Z;
return EXIT_SUCCESS;
}
2)设置matlab路径
sudo gedit /etc/profile
添加:
MATLAB=/opt/MATLAB/R2012a
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATLAB/bin/glnxa64
export LIBRARY_PATH=$LIBRARY_PATH:$MATLAB/bin/glnxa64