matlab mex dll,Visual Studio创建Matlab mex(dll)函数

本文主要介绍在Windows XP 32bit操作系统下,利用Visual Studio 2005,如何生成Matlab可执行的mex(dll)文件。(1). 首先,使用Visual Studio 2005编译生成动态链接库dll文件。(2). 然后,在Matlab 2009b中调用生成的mex(dll)文件。

1). 打开Visual Studio 2005,建立一个Win32 Project,项目名为test_matlab_3

194ade84546a862938beed29e764e287.png

2). 项目类型设定时选择DLL,并选定Export symbol和Empty project,注意要先选Export symbol再选Empty project。

3). 然后打开project -> add new item,添加C++ file 文件名为mexFunction.cpp。文件代码后附。

b6a727271075473f65bfce351afbc3a9.png

   4). 然后打开tool -> option -> Projects and Solutions -> VC++ Directories -> Include files 增加matlab头文件目录地址。"C:\Program Files\MATLAB\R2009b\extern\include"。由于最近经常使用matlab,就把头文件目录添加到VC设置中。如果只是偶尔使用matlab,可以把头文件目录加到project -> test_matlab_3 properties -> C++ -> General -> Additional Include Directories 中。b3c5c0304cf1d71b040520bd2445f65f.png

5). 然后打开tool -> option -> Projects and Solutions -> VC++ Directories -> Library files 增加matlab库文件目录地址。"C:\Program Files\MATLAB\R2009b\extern\lib\win32\microsoft"。如果只是偶尔使用matlab,可以把库文件目录加到project -> test_matlab_3 properties -> Linker -> General -> Additional Library Directories 中。

ffe55e23ebda14b4f2c75bc045e104bf.png

6). 然后打开project -> test_matlab_3 properties -> C++ -> Preprocessor 添加 TEST_MATLAB_3_EXPORTS。

185af09b9e9233bf234354985e2eccde.png  

7). 然后打开project -> test_matlab_3 properties -> C++ -> Code Generation -> Runtime Library,选择 Multi-threaded Debug (/MTd)。

4f8da3508c63a801c8eea2245b8acb41.png

8). 然后打开project -> test_matlab_3 properties -> Linker -> Input -> Additional Dependencies中添加 libmx.lib,libmat.lib,libmex.lib,libeng.lib。

9). 然后打开project -> test_matlab_3 properties -> Linker -> Input -> Module Definition File中添加 .\mexFunction.def。6b7a0a2aa47f6ef06b3780fdabe155f7.png

10). 然后打开project -> add new item,添加Module-Definition File 文件名为mexFunction.def。文件代码后附。9d420ed5369ded4f7ea6a447f47e025f.png

11). 然后打开project -> add new item,添加Header File 文件名为mexFunction.h。文件代码后附。

341fd4abb4ec1c11860f13674ef1f1b8.png

12). 编译,链接,生成test_matlab_3.dll文件,也就是我们想要的mex文件。

13). 将生成的文件拷贝到matlab目录下,执行"test_matlab_3(1,'Panpan Hu')",返回如下结果

7911063e65cb6bf84b8912a0a03d1967.png

注意:本质上来讲mex和dll没有区别,只是两个不同的后缀名。Matlab2010b以后版本可能不支持调用dll为后缀的mex文件。消息来源如下

http://www.mathworks.com/help/techdoc/matlab_external/bsehn8g.html

A MEX-file is a shared library dynamically loaded at runtime. Shared libraries are sometimes called .dll files, for dynamically-linked library. MEX-files have a platform-dependent extension, which the mex function automatically assigns.

On 32-bit Windows platforms, the extension is .mexw32. MATLAB has supported .dll as a secondary MEX-file extension since Version 7.1 (R14SP3). In Version 7.7 (R2008b), if you used the -outputswitch to create a MEX-file with a .dll extension, MATLAB displayed a warning message that such usage is being phased out.

In MATLAB Version 7.10 (R2010a), you can no longer create a MEX-file with a .dll file extension. If you try to, MATLAB creates the MEX-file with the proper extension and displays the following warning:

Warning: Output file was specified with file extension, ".dll", which is not a proper MEX-file extension. The proper extension for this platform, ".mexw32", will be used instead.

MATLAB continues to execute a MEX-file with a .dll extension, but future versions of MATLAB will not support this extension.

本文参考如下网络资源

http://blog.sina.com.cn/s/blog_4d1865f00100o2ul.html

http://www.engineering.uiowa.edu/~dip/lecture/C++_with_Matlab.pdf

附录1 mexFunction.cpp

#include "mexFunction.h"

#include

#include "stdlib.h"

#include

using namespace std;

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

{

double *Encoder_Decoder_db = NULL;

string Path_Str="";     // the path of the bands

unsigned int bufferlength = mxGetM(prhs[1])*mxGetN(prhs[1])+1;

char *Path_Str_ch = new char[bufferlength];

short Encoder_Decoder;  // 0: encoder, 1: decoder

Encoder_Decoder_db = mxGetPr(prhs[0]);

mxGetString(prhs[1], Path_Str_ch, bufferlength);

Encoder_Decoder = (short) *Encoder_Decoder_db;

Path_Str = Path_Str + Path_Str_ch;

mexPrintf("\nBegin of Test-Zhao Wang 6.2.2011\n");

mexPrintf("%d, %s\n", Encoder_Decoder, Path_Str_ch);

mexPrintf("End of Test-Zhao Wang 6.2.2011\n");

}

附录2 mexFunction.h

#include "matrix.h"

#include "mex.h"

#define TEST_MATLAB_3_EXPORTS

#ifdef TEST_MATLAB_3_EXPORTS

#define MEX_FUNCTION_API __declspec(dllexport)

#else

#define MEX_FUNCTION_API __declspec(dllimport)

#endif

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

附录3 mexFunction.def

LIBRARY    "test_matlab_3"

EXPORTS

mexFunction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值