您的位置:首页 > 编程语言 > MATLAB

matlab调用c++,应该用mex把cpp编译成 .mex文件供MATLAB在命令行方式下调用吧

2016-06-24 19:24 423 查看
看下面这片文章

文章: 摘自北京理工大学BBS

MATLAB是什么东东?不用我多说了,大批的高手会告诉你它的无数好处。但是MATLAB程序存在的一般问题是:

1、运算速度较慢,特别是有for i=1:???

for j=1:??? ...... end

end时,那你就死定了。

2、无法脱离MATLAB环境运行,虽然你可以用MATLAB自带的工具把M文件转成可执行程序,但是执行时,你只能看到一个黑黑的窗口,难道,这就是......Windows? $%&%^$##%&。

3、GUI功能差。许多大侠不愿意看到2中所述的DOS-Windows界面,于是使用了MATLAB带的GUI生成工具。(不要扔臭鸡蛋!我知道你用Graphic Editor改了一下界面,发现以前的工作全都白做了――因为被覆盖了,西西)但是MATLAB提供的控件和事件实在有限。GUI界面做好后放在机器里,向尚未进实验室的师妹(们)炫耀一下,还勉强可以增加一些魅力值。如果被专家看到了,西西。只怕效果不太美妙。 因此,如果能够实现MATLAB与VC或者BC或者C++BUILDER等可视化设计语言的交互,提高速度,美化界面,使程序更符合Windows的规范,同时又利用MATLAB的强大功能,对任何人来说都很有意义。

我在课题中涉及到了部分MATLAB与C++的接口知识,在这里总结出来,算是抛砖引玉吧。其中错误以及偏颇的地方,尚请各位大侠以及高手多多指教,谢谢! (一)接口种类

用C++(或者C)语言操作MATLAB,有三种途径: ?MEX文件

在MATLAB中可调用的C或Fortran语言程序称为MEX文件。MATLAB可以直接把MEX文件视为它的内建函数进行调用。MEX文件是动态链接的子例程,MATLAB解释器可以自动载入并执行它。MEX文件主要有以下用途:

对于大量现有的C或者Fortran程序可以无须改写成MATLAB专用的M文件格式而在MATLAB中执行。

对于那些MATLAB运算速度过慢的算法,可以用C或者Frotran语言编写以提高效率。 ?MAT文件应用程序

MAT文件是MATLAB专用的用于保存数据至磁盘和向MATLAB导入、从MATLAB导出数据的数据文件格式。MAT文件提供了一种简便的机制,它允许你在两个平台之间以灵活的方式移动数据。而且,它还提供了一种途径来向其它单机MATLAB应用导入或者导出数据。 为了简化在MATLAB环境之外对MAT文件的使用,MATLAB给出了一个操作例程库,通过它,我们可以使用C/C++或者Fortran程序读写MAT文件。 ?引擎应用程序

MATLAB提供了一系列的例程使得别的程序可以调用MATLAB,从而把MATLAB用作一个计算引擎。MATLAB引擎程序指的是那些通过管道(在UNIX系统中)或者ActiveX(在Windows系统中)与独立MATLAB进程进行通信的C/C++或者Fortran程序。

MATLAB同样提供了一个函数库来启动或者结束MATLAB进程、与MATLAB交换数据以及

var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

发送MATLAB命令。 (二)MEX文件 1、一个MEX的例子

#include "mex.h" /*

* timestwo.c - example found in API guide *

* Computational function that takes a scalar and doubles it. *

* This is a MEX-file for MATLAB.

* Copyright (c) 1984-1998 The MathWorks, Inc. */

/* $Revision: 1.5 $ */

/* 本MEX文件的目的是实现timestwo的功能

void timestwo(double y[], double x[])是你的C++函数 */

void timestwo(double y[], double x[]) {

y[0] = 2.0*x[0]; }

/*下面这个mexFunction的目的是使MATLAB知道如何调用这个timestwo函数*/ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )

/* nlhs是MATLAB命令行方式下输出参数的个数; *plhs[]是MATLAB命令行方式下的输出参数; nrhs是MATLAB命令行方式下输入参数的个数; *prhs[]是MATLAB命令行方式下的输入参数; */

{

double *x,*y; int mrows,ncols;

/* Check for proper number of arguments. */ if(nrhs!=1) {

mexErrMsgTxt("One input required."); } else if(nlhs>1) {

mexErrMsgTxt("Too many output arguments"); }

/* 在MATLAB命令行方式下,本MEX文件的调用格式是y=timestwo(x) 输入参数(x)个数=1,输出参数(y)个数=1,所以在程序一

开始就检查nrhs是否=1以及nlhs是否>1(因为MATLAB有一个缺省 输出参数ans,所以nlhs可以=0 */ 输出参数ans,所以nlhs可以=0 */

var cpro_psid ="u2572954"; var cpro_pswidth =966; var cpro_psheight =120;

/* The input must be a noncomplex scalar double.*/

mrows = mxGetM(prhs[0]); /* 获得输入矩阵的行数 */ ncols = mxGetN(prhs[0]); /* 获得输入矩阵的列数 */

if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || !(mrows==1 && ncols==1) ) {

mexErrMsgTxt("Input must be a noncomplex scalar double.");

} /* 判断输入矩阵是否是double类,以及它是否只包括单个元素 */

/* 为输出创佳一个矩阵,显然这个矩阵也应该是1x1的 */ plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL); /* 获得指向输入/输出矩阵数据的指针 */ x = mxGetPr(prhs[0]); y = mxGetPr(plhs[0]);

/* 调用C++函数timestwo(y,x) */

timestwo(y,x); }

把上面这个文件用MEX编译后,MATLAB命令行下调用的实例 x = 2;

y = timestwo(x) y = y = 4

2、MEX文件的编程规则

(1)编制自己的C++算法程序

(2)紧跟着定义mexFunction函数,mexFunction的定义法唯一: 它只能是如下形式:

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )

其名称和参数类型不许有任何改变,在mexFunciton函数中可以调用你刚定义好的C++程序。

3、MEX文件的编译

MATLAB提供了专门编译MEX文件的工具:mex,它可以把你做好的C++源程序编译成 .mex文件供MATLAB在命令行方式下调用。调用时输入你的C++函数名(上例中为timestwo)。具体mex的设置和使用方法可以在MATLAB命令行方式下用help mex命令获得。

(三)MAT文件 1、概述

.MAT文件是MATLAB专用的数据存储格式,由于MATLAB提供了一套可供MATLAB调用的API函数集,所以我们完全可以在C++中访问.MAT文件。这样做的意义是:你可以把计算过程留给MATLAB,而用C++对计算结果进行分析或者可视化处理。

2、接口

n=pLCMShdr->TrueHdr.nArrayDim[1]; file://获得原矩阵列数。 nzmax=0;

for(i=0;i<m*n;i++) { if(pData!=0)

nzmax++; nzmax++;

} file://计算数据中非零元个数。 if(nzmax<n) nzmax=n;

pa=mxCreateSparse(m,n,nzmax,mxREAL); /*创建一个空的稀疏矩阵pa。*/

mxSetName(pa,pLCMShdr->TrueHdr.szArrayName); /*为稀疏矩阵pa设置名称。*/ pr=mxGetPr(pa); file://获得pa的pr指针。 ir=mxGetIr(pa); file://获得pa的ir指针。 jc=mxGetJc(pa); file://获得pa的jc指针。

k=0;

for(j=0;j<n;j++)

{ jc[j]=k; file://jc[j]:截至到第j列非零元的个数。

for(i=0;i { if(pData!=0) file://如果第j列第i行的元素是个非零元。

{ ir[k]=i; file://记录下第k个非零元的行号。 k++; } }

pData+=m; file://移动pData指针到下一列。

}

jc
=k; file://jc
等于矩阵中非零元的个数。

matPutArray(pmat,pa); file://把稀疏矩阵pa存入MAT文件pmat。 mxDestroyArray(pa); file://从内存中撤销矩阵pa。 (五)引擎应用程序 1、简介

引擎应用程序的实质是把MATLAB做为一个引擎,它允许从你自己的C++程序调用这个引擎。在运行时,引擎作为一个进程单独运行,你的C++程序也作为一个进程单独运行,二者可以通过进程间的通信机制进行交互。 2、引擎库

MATLAB引擎库包含了若干个控制MATLAB引擎的函数,如下所示: engOpen 启动MATLAB引擎 engClose 关闭MATLAB引擎

engGetArray 从MATLAB引擎中获取一个MATLAB矩阵 engPutArray 向MATLAB引擎发送一个MATLAB矩阵 engEvalString 执行于一个MATLAB命令

engOutputBuffer 创建一个存储MATLAB文本输出的缓冲区

同时,引擎应用程序还可以使用前面提到的API函数。 3、一个例子

从这个示例中,我们看出引擎应用程序是如何编制的: /* $Revision: 1.3 $ */ /*

* engdemo.c *

* This is a simple program that illustrates how to call the * MATLAB engine functions from a C program. *

* Copyright (c) 1996-1998 The MathWorks, Inc. * All rights reserved */

#include #include #include

#include "engine.h" #define BUFSIZE 256 int main() {

Engine *ep;

mxArray *T = NULL, *result = NULL; char buffer[BUFSIZE];

double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }; 8.0, 9.0 }; 6-6 /*

* Start the MATLAB engine locally by executing the string * "matlab". *

* To start the session on a remote host, use the name of * the host as the string rather than \0 *

* For more complicated cases, use any string with whitespace, * and that string will be executed literally to start MATLAB. */

if (!(ep = engOpen("\0"))) {

fprintf(stderr, "\nCan't start MATLAB engine\n"); return EXIT_FAILURE; } /*启动MATLAB引擎*/ /*

* PART I *

* For the first half of this demonstration, we will send data * to MATLAB, analyze the data, and plot the result. */ /* /*

* Create a variable for our data. */

T = mxCreateDoubleMatrix(1, 10, mxREAL); /*创建一个矩阵*/ mxSetName(T, "T"); /*设置矩阵的名字为“T”*/

memcpy((void *)mxGetPr(T), (void *)time, sizeof(time)); /*向矩阵“T”赋值*/ /*

* 把矩阵“T”置入MATLAB引擎

*/

engPutArray(ep, T) /*

* Evaluate a function of time, distance = (1/2)g.*t.^2 * (g is the acceleration due to gravity). */

engEvalString(ep, "D = .5.*(?9.8).*T.^2;"); /*执行MATLAB 命令:D = .5.*(?9.8).*T.^2;*/ /*

* 绘制图象.

*/

engEvalString(ep, "plot(T,D);"); /*执行MATLAB命令:绘图*/

engEvalString(ep, "title('Position vs. Time for a falling object');"); /*执行MATLAB命令:给图象加标题*/

engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "ylabel('Position (meters)');"); /*执行MATLAB命令:设置Y轴 坐标*/

/*

* Use fgetc() to make sure that we pause long enough to be * able to see the plot. */

printf("Hit return to continue\n\n"); fgetc(stdin); /*

* We're done for Part I! Free memory, close MATLAB engine. */

printf("Done for Part I.\n");

mxDestroyArray(T); /*从内存中撤销矩阵“T”*/

engEvalString(ep, "close;"); /*关闭刚才显示图象的窗口*/ /*

* PART II

*

* For the second half of this demonstration, we will request * a MATLAB string, which should define a variable X. MATLAB * will evaluate the string and create the variable. We * will then recover the variable, and determine its type. */ */ /*

* Use engOutputBuffer to capture MATLAB output, so we can * echo it back. */

engOutputBuffer(ep, buffer, BUFSIZE); /*构建MATLAB文本输入缓冲区*/ while (result == NULL) { char str[BUFSIZE]; /*

* Get a string input from the user. */

printf("Enter a MATLAB command to evaluate. This command should\n");

printf("create a variable X. This program will then determine\n");

printf("what kind of variable you created.\n"); printf("For example: X = 1:5\n");

printf(">> "); /*要求用户输入一个MATLAB命令*/ fgets(str, BUFSIZE?1, stdin); /*获得用户输入*/

/*

* Evaluate input with engEvalString. */

engEvalString(ep, str); /*执行用户输入的MATLAB命令*/ engEvalString(ep, str); /*执行用户输入的MATLAB命令*/ /*

* Echo the output from the command. First two characters * are always the double prompt (>>). */

printf("%s", buffer+2); /*显示该MATLAB命令的执行情况*/

/*

* Get result of computation. */

printf("\nRetrieving X...\n");

if ((result = engGetArray(ep,"X")) == NULL) /*判断是否可以从MATLAB 引擎中获得矩阵“X”*/

printf("Oops! You didn't create a variable X.\n\n"); else

printf("X is class %s\t\n", mxGetClassName(result)); /*显示矩阵“X”

的类型*/

} /* while(result==NULL)*/ /*

* We're done! Free memory, close MATLAB engine and exit. */

printf("Done!\n");

mxDestroyArray(result); /*从内存中撤销矩阵“T”*/ engClose(ep); /*关闭MATLAB引擎*/ return EXIT_SUCCESS; /*返回*/

}

4、引擎应用程序的编译

对于象上例中的控制台程序,可以在MATLAB命令行中直接使用带-f参数的mex命令编译。 如果在普通win32 application中使用MATLAB引擎,情况则比较复杂。在Windows中,MATLAB引擎是通过ActiveX被调用的。因此你需要先Create一个OLE Automation Sever和一个OLE Client,然后通过OLE方式调用这个MATLAB引擎。具体做法可参阅相关MATLAB随机文档。 5、总结

MATLAB引擎的调用与其它引擎(例如数据库引擎)的调用很类似,其步骤是联接\启动引擎,然后向引擎发送命令,获得引擎处理结果。

结束语

上面简要介绍了MATLAB与C++的几种接口方式,我们可以根据要求的不同采用相应的方式。

此外,MATLAB还提供了一个数学库,由此数学库,我们可以获得对MATLAB内部命令更多的访问权和更灵活的访问方式。具体内容可参考MATLAB的相关随机文档。 ?参考文献

1、MATLAB随机文档:apiguide.pdf 2、MATLAB随机文档:apiref.pdf

3、MATLAB随机文档:c_math_ref1.pdf ?致谢

感谢loverboy的鼓励,他声称要把这些帖子放到他的主页上,才使得我能够在短时间完成这篇接口问题的文章,西西。 ?备注

mex编译器在VC6下的设置可参阅本板19#文 Bluesky的意见:

VC程序员最好用visual matcom。VB用matrixVB mathtool在主页上提供免费试用,快去下吧。matlab的功能可在你的VC,VB中实现,而且只需两个dll即可发行。

n=pLCMShdr->TrueHdr.nArrayDim[1]; file://获得原矩阵列数。 nzmax=0;

for(i=0;i<m*n;i++) { if(pData!=0)

nzmax++; nzmax++;

} file://计算数据中非零元个数。 if(nzmax<n) nzmax=n;

pa=mxCreateSparse(m,n,nzmax,mxREAL); /*创建一个空的稀疏矩阵pa。*/

mxSetName(pa,pLCMShdr->TrueHdr.szArrayName); /*为稀疏矩阵pa设置名称。*/ pr=mxGetPr(pa); file://获得pa的pr指针。 ir=mxGetIr(pa); file://获得pa的ir指针。 jc=mxGetJc(pa); file://获得pa的jc指针。

k=0;

for(j=0;j<n;j++)

{ jc[j]=k; file://jc[j]:截至到第j列非零元的个数。

for(i=0;i { if(pData!=0) file://如果第j列第i行的元素是个非零元。

{ ir[k]=i; file://记录下第k个非零元的行号。 k++; } }

pData+=m; file://移动pData指针到下一列。

}

jc
=k; file://jc
等于矩阵中非零元的个数。

matPutArray(pmat,pa); file://把稀疏矩阵pa存入MAT文件pmat。 mxDestroyArray(pa); file://从内存中撤销矩阵pa。 (五)引擎应用程序 1、简介

引擎应用程序的实质是把MATLAB做为一个引擎,它允许从你自己的C++程序调用这个引擎。在运行时,引擎作为一个进程单独运行,你的C++程序也作为一个进程单独运行,二者可以通过进程间的通信机制进行交互。 2、引擎库

MATLAB引擎库包含了若干个控制MATLAB引擎的函数,如下所示: engOpen 启动MATLAB引擎 engClose 关闭MATLAB引擎

engGetArray 从MATLAB引擎中获取一个MATLAB矩阵 engPutArray 向MATLAB引擎发送一个MATLAB矩阵 engEvalString 执行于一个MATLAB命令

engOutputBuffer 创建一个存储MATLAB文本输出的缓冲区

同时,引擎应用程序还可以使用前面提到的API函数。 3、一个例子

从这个示例中,我们看出引擎应用程序是如何编制的: /* $Revision: 1.3 $ */ /*

* engdemo.c *

* This is a simple program that illustrates how to call the * MATLAB engine functions from a C program. *

* Copyright (c) 1996-1998 The MathWorks, Inc. * All rights reserved */

#include #include #include

#include "engine.h" #define BUFSIZE 256 int main() {

Engine *ep;

mxArray *T = NULL, *result = NULL; char buffer[BUFSIZE];

double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }; 8.0, 9.0 }; 6-6 /*

* Start the MATLAB engine locally by executing the string * "matlab". *

* To start the session on a remote host, use the name of * the host as the string rather than \0 *

* For more complicated cases, use any string with whitespace, * and that string will be executed literally to start MATLAB. */

if (!(ep = engOpen("\0"))) {

fprintf(stderr, "\nCan't start MATLAB engine\n"); return EXIT_FAILURE; } /*启动MATLAB引擎*/ /*

* PART I *

* For the first half of this demonstration, we will send data * to MATLAB, analyze the data, and plot the result. */ /* /*

* Create a variable for our data. */

T = mxCreateDoubleMatrix(1, 10, mxREAL); /*创建一个矩阵*/ mxSetName(T, "T"); /*设置矩阵的名字为“T”*/

memcpy((void *)mxGetPr(T), (void *)time, sizeof(time)); /*向矩阵“T”赋值*/ /*

* 把矩阵“T”置入MATLAB引擎

*/

engPutArray(ep, T) /*

* Evaluate a function of time, distance = (1/2)g.*t.^2 * (g is the acceleration due to gravity). */

engEvalString(ep, "D = .5.*(?9.8).*T.^2;"); /*执行MATLAB 命令:D = .5.*(?9.8).*T.^2;*/ /*

* 绘制图象.

*/

engEvalString(ep, "plot(T,D);"); /*执行MATLAB命令:绘图*/

engEvalString(ep, "title('Position vs. Time for a falling object');"); /*执行MATLAB命令:给图象加标题*/

engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "ylabel('Position (meters)');"); /*执行MATLAB命令:设置Y轴 坐标*/

/*

* Use fgetc() to make sure that we pause long enough to be * able to see the plot. */

printf("Hit return to continue\n\n"); fgetc(stdin); /*

* We're done for Part I! Free memory, close MATLAB engine. */

printf("Done for Part I.\n");

mxDestroyArray(T); /*从内存中撤销矩阵“T”*/

engEvalString(ep, "close;"); /*关闭刚才显示图象的窗口*/ /*

* PART II

*

* For the second half of this demonstration, we will request * a MATLAB string, which should define a variable X. MATLAB * will evaluate the string and create the variable. We * will then recover the variable, and determine its type. */ */ /*

* Use engOutputBuffer to capture MATLAB output, so we can * echo it back. */

engOutputBuffer(ep, buffer, BUFSIZE); /*构建MATLAB文本输入缓冲区*/ while (result == NULL) { char str[BUFSIZE]; /*

* Get a string input from the user. */

printf("Enter a MATLAB command to evaluate. This command should\n");

printf("create a variable X. This program will then determine\n");

printf("what kind of variable you created.\n"); printf("For example: X = 1:5\n");

printf(">> "); /*要求用户输入一个MATLAB命令*/ fgets(str, BUFSIZE?1, stdin); /*获得用户输入*/

/*

* Evaluate input with engEvalString. */

engEvalString(ep, str); /*执行用户输入的MATLAB命令*/ engEvalString(ep, str); /*执行用户输入的MATLAB命令*/ /*

* Echo the output from the command. First two characters * are always the double prompt (>>). */

printf("%s", buffer+2); /*显示该MATLAB命令的执行情况*/

/*

* Get result of computation. */

printf("\nRetrieving X...\n");

if ((result = engGetArray(ep,"X")) == NULL) /*判断是否可以从MATLAB 引擎中获得矩阵“X”*/

printf("Oops! You didn't create a variable X.\n\n"); else

printf("X is class %s\t\n", mxGetClassName(result)); /*显示矩阵“X”

的类型*/

} /* while(result==NULL)*/ /*

* We're done! Free memory, close MATLAB engine and exit. */

printf("Done!\n");

mxDestroyArray(result); /*从内存中撤销矩阵“T”*/ engClose(ep); /*关闭MATLAB引擎*/ return EXIT_SUCCESS; /*返回*/

}

4、引擎应用程序的编译

对于象上例中的控制台程序,可以在MATLAB命令行中直接使用带-f参数的mex命令编译。 如果在普通win32 application中使用MATLAB引擎,情况则比较复杂。在Windows中,MATLAB引擎是通过ActiveX被调用的。因此你需要先Create一个OLE Automation Sever和一个OLE Client,然后通过OLE方式调用这个MATLAB引擎。具体做法可参阅相关MATLAB随机文档。 5、总结

MATLAB引擎的调用与其它引擎(例如数据库引擎)的调用很类似,其步骤是联接\启动引擎,然后向引擎发送命令,获得引擎处理结果。

结束语

上面简要介绍了MATLAB与C++的几种接口方式,我们可以根据要求的不同采用相应的方式。

此外,MATLAB还提供了一个数学库,由此数学库,我们可以获得对MATLAB内部命令更多的访问权和更灵活的访问方式。具体内容可参考MATLAB的相关随机文档。 ?参考文献

1、MATLAB随机文档:apiguide.pdf 2、MATLAB随机文档:apiref.pdf

3、MATLAB随机文档:c_math_ref1.pdf ?致谢

感谢loverboy的鼓励,他声称要把这些帖子放到他的主页上,才使得我能够在短时间完成这篇接口问题的文章,西西。 ?备注

mex编译器在VC6下的设置可参阅本板19#文 Bluesky的意见:

VC程序员最好用visual matcom。VB用matrixVB mathtool在主页上提供免费试用,快去下吧。matlab的功能可在你的VC,VB中实现,而且只需两个dll即可发行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: