转自:http://blog.sina.com.cn/s/blog_78bd53ef0100r81d.html
在NS2中,可以通过连接库,来调用Matlab函数。
通常的做法是:先在Matlab M文件中编写你想要实现的函数,将它用Matlab编译器编译成共享的lib库;然后在NS2 C++代码中添加这些库。
下面通过一个例子来说明。
环境为:Matlab Compiler 4.1,gcc 3.2 和 NS2.27
第一步:用M文件编写Matlab函数;如下:
function a = addmatrix(a1, a2)
�DMATRIX Add two matrices
% This function adds the two matrices passed as input. This function is
% used to demonstate the functionality of MATLAB Compiler. Refer to the
% shared library section of MATLAB Compiler for more information.
% Copyright 2003 The MathWorks, Inc.
a = a1 + a2;
保存为addmatrix,m。
第二步:在Matlab中,运行命令: mmc -B csharedlib:libmarix addmatrix.m -V
在ns-2.27文件夹新建matlab文件夹,然后复制Matlab产生的文件libmatrix.h, libmatrix.ctf, libmatrix.so,到新建的文件夹中。
第三步:在ns-2.27/matlab中,新建两个文件,在文件中定义matlab类,文件如下:
#include
#include "mclmcr.h"
#include "libmatrix.h"
class Matlab {
public:
Matlab();
~Matlab();
void Addmatrix(const double matrix1[], const int matrix1_r, const int matrix1_c,
const double matrix2[], const int matrix2_r, const int matrix2_c,
double *result, int* result_r, int* result_c );
};
保存为matlab.h。
#include "matlab.h"
// the constructor
Matlab::Matlab() {
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}
if (!libmatrixInitialize()){
fprintf(stderr,"Could not initialize the library.\n");
exit(1);
}
}
// the destructor
Matlab::~Matlab(){
libmatrixTerminate();
libtestTerminate();
mclTerminateApplication();
}
void
Matlab::Addmatrix(const double matrix1[], const int matrix1_r, const int matrix1_c,
const double matrix2[], const int matrix2_r, const int matrix2_c,
double *result, int *result_r, int *result_c )
{
mxArray *in1, *in2;
mxArray *out = NULL;
double *data;
int i, j;
in1 = mxCreateDoubleMatrix(matrix1_r, matrix1_c, mxREAL);
in2 = mxCreateDoubleMatrix(matrix2_r, matrix2_c, mxREAL);
memcpy(mxGetPr(in1), matrix1, matrix1_r*matrix1_c*sizeof(double));
memcpy(mxGetPr(in2), matrix2, matrix2_r*matrix2_c*sizeof(double));
mlfAddmatrix(1, &out, in1, in2);
*result_r = mxGetM(out);
*result_c = mxGetN(out);
data = mxGetPr(out);
for(i=0;i<*result_r;i++)
{
for(j=0;j<*result_c;j++)
result[i*(*result_c)+j]=data[i*(*result_c)+j];
}
mxDestroyArray(in1); in1=0;
mxDestroyArray(in2); in2=0;
}
保存为matlab.cc.
第四步:在你需要调用Matlab::Addmatrix的类中添加相关代码,记得包含matlab.h文件。
第五步:在你的library path中添加下面的路径:
setenv LD_LIBRARY_PATH
/bin/glnx86:
/sys/os/glnx86:
/sys/java/jre/glnx86/jre1.4.2/lib/i386/client:
/sys/java/jre/glnx86/jre1.4.2/lib/i386:
/sys/opengl/lib/glnx86:${LD_LIBRARY_PATH}
第六步:对ns-2.27/Makefile.in进行修改
INCLUDES = \
-I. @V_INCLUDE_X11@ \
@V_INCLUDES@ \
-I./tcp -I./sctp -I./common -I./link -I./queue \
-I./adc -I./apps -I./mac -I./mobile -I./trace \
-I./routing -I./tools -I./classifier -I./mcast \
-I./diffusion3/lib/main -I./diffusion3/lib \
-I./diffusion3/lib/nr -I./diffusion3/ns \
-I./diffusion3/filter_core -I./asim/ -I./qs \
-I/usr/local/matlab7/extern/include
#Please replace the path to matlab header files
......
LIB = \
@V_LIBS@ \
@V_LIB_X11@ \
@V_LIB@ -L./matlab -lmatrix \
-lm @LIBS@
......
OBJ_CC = \
......
matlab/matlab.o \
@V_STLOBJ@
第七步:运行 ./configure
make clean
make depend (会有错误提供,不理会)
make