vs&matlab混合编程-- - mexw64



转载:http://blog.csdn.net/wak0408/article/details/38272163


这个文章的流程是,在vs里面生成matlab可以读懂的文件(mexw**),当然也可以在matlab里面编译 .c/.cpp,直接生成(mexw**),matlab的.m文件就可以直接调用了。

vs&matlab混合编程,例如文章最后的两个参考文献,但是参考文献都是旧版本的matlab和vs,有些新的特新没有体现出来,所以我还是总结一下搭建方法。

       首先说明一下mexw64,最简单的理解,mexw64就是dll。matlab较旧的版本可以直接调用dll,到了新版本好像不行了,经过各种百度,matlab2011需要调用mexwXX,这里的XX可以是32和64,分别代表32位和64位。matlab可以将这个dll当作一个函数调用,如图:

这个是matlab调用的效果图,接下来一步一步的说一下如何搭建。

在搭建之前,先详细说明一下我的环境。

matlab版本:


安装目录:D:\Program Files\MATLAB 

VS版本:


VS安装目录:C:\Program Files (x86)\Microsoft Visual Studio 12.0


1)创建VS工程


2)配置工程,选择应用程序类型为DLL,附加选项中先选择导出符号后选择空项目。


3)为工程添加test.h test.cpp test.def三个文件。


test.h

  1. #include "matrix.h"  
  2. #include "mex.h"  
  3. #define TESTMATLAB_EXPORTS  
  4. #ifdef TESTMATLAB_EXPORTS  
  5. #define MEX_FUNCTION_API __declspec(dllexport)  
  6. #else  
  7. #define MEX_FUNCTION_API __declspec(dllimport)  
  8. #endif  
  9. MEX_FUNCTION_API void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[]);  
#include "matrix.h"
#include "mex.h"
#define TESTMATLAB_EXPORTS
#ifdef TESTMATLAB_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[]);

test.cpp

  1. #include "test.h"  
  2. #include <string>  
  3. #include "stdlib.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])  
  7. {  
  8.     double *Encoder_Decoder_db = NULL;  
  9.     string Path_Str = "";     // the path of the bands  
  10.     unsigned int bufferlength = mxGetM(prhs[1])*mxGetN(prhs[1]) + 1;  
  11.     char *Path_Str_ch = new char[bufferlength];  
  12.     short Encoder_Decoder;  // 0: encoder, 1: decoder  
  13.     Encoder_Decoder_db = mxGetPr(prhs[0]);  
  14.     mxGetString(prhs[1], Path_Str_ch, bufferlength);  
  15.     Encoder_Decoder = (short)*Encoder_Decoder_db;  
  16.     Path_Str = Path_Str + Path_Str_ch;  
  17.     mexPrintf("\nBegin of Test pagekpang 2014-7-29 \n");  
  18.     mexPrintf("%d, %s\n", Encoder_Decoder, Path_Str_ch);  
  19.     mexPrintf("End of Test pagekpang 2014-7-29\n");  
  20. }  
#include "test.h"
#include <string>
#include "stdlib.h"
#include <iostream>
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 pagekpang 2014-7-29 \n");
	mexPrintf("%d, %s\n", Encoder_Decoder, Path_Str_ch);
	mexPrintf("End of Test pagekpang 2014-7-29\n");
}


test.def
  1. LIBRARY    "testp"  
  2. EXPORTS  
  3.         mexFunction  
LIBRARY    "testp"
EXPORTS
        mexFunction

这时候会发现,整个项目红了一大票,到处报错。主要是因为没有添加include和lib,接下来就需要对工程进行配置。

4)在自己的项目中右键-》属性。找到 配置属性 底下的 VC++ 目录

分别设置包含目录和库目录,将matlab的函数导入到VS。

(1)包含目录

(2)库目录


5)设置运行库



6)添加lib



7)模块定义文件



8)添加x64平台

(1)选择配置管理器


(2)选择新建


(3)输入参数如下



9)编译



10)拷贝工程目录下的x64\Debug\TestMatlab.dll到Matlab的工作目录中,并且更名为TestMatlab.mexw64,就可以运行了。



文中大部分工作我也是参考了《Visual Studio创建Matlab mex(dll)函数》,只是有些地方配置不太一样,改变了一下参数。下一步要完善的是hackrf的操作函数,将libhackrf植入到Matlab中。


参考文献

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

http://www.360doc.com/content/14/0411/17/1944636_368074271.shtml

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值