c怎么调用matlab dll,matlab和c++调用DLL方法(最新整理)

《matlab和c++调用DLL方法(最新整理)》由会员分享,可在线阅读,更多相关《matlab和c++调用DLL方法(最新整理)(6页珍藏版)》请在人人文库网上搜索。

1、Matlab 和 C+调用 DLL 方法Matlab 可以调用外部 DLL,如用 C 或 C+写的 DLL。本文分三个部分介绍 Matlab 和 C+调用 DLL 方法。一、C+写 dll 方法二、C+调用 dll三、Matlab 调用 dll一、C+写 DLL 方法在 CPP 文件中和普通 CPP 没什么区别,在 H 文件中加上需要导出的函数名,格式如下:extern C intdeclspec(dllexport)add(int x, int y);蓝色部分是函数返回的类型,红色部分是要导出的函数, 就这么简单。下面是两个整数相加的代码:main.h:extern C intdeclspe。

2、c(dllexport)add(int x, int y);main.cpp:#include main.hint add(int x, int y)return x + y;编译即可得到 dll 文件,我的工程名叫 mydll,因此得到的是 mydll.dll二、C+调用 DLL 方法将 DLL 文件拷入到源文件夹中,在 CPP 代码中引入windows.h 文件,否则有些方法找不到,用指针来接受参数。装载 DLL,用函数名,卸载 DLL。代码如下:#include #include using namespace std; int main(void)typedef double(*pad。

3、d)(double a,double b); double r;HINSTANCE hDLL;padd add; hDLL=LoadLibrary(mydll.dll); add=(padd)GetProcAddress(hDLL,add); r=add(5.3,8.6);cout loadlibrary(mydll.dll,main.h) libfunctions(mydll) 会得到如下信息: Functions in library mydll: add接着输入: calllib(mydll,add,3,5)ans =8调用成功!可以看出 matlab 调用 DLL 主要用了 loadl。

4、ibrary和 calllib 两个函数,搞定!Mex 文件调用 DLL这种方法稍微麻烦一些,但是一劳永逸。主要是通过 mexfunction 函数。代码如下: Test.cpp:#include mex.hvoid timestwo(double y,double x)y0=2.0*x0;voidmexFunction(intnlhs,mxArray*plhs,intnrhs,const mxArray *prhs)double *x,*y; int mrows,ncols;if(nrhs!=1)mexErrMsgTxt(One input required.);elseif(nlhs1)m。

5、exErrMsgTxt(Toomanyoutput arguments);mrows=mxGetM(prhs0); ncols=mxGetN(prhs0);if(!mxIsDouble(prhs0)|mxIsComplex(prhs0)|!(mrows=1& &ncols=1)mexErrMsgTxt(Input must be a noncomplex scalardouble.);plhs0=mxCreateDoubleMatrix(mrows,ncols,mxREAL); x=mxGetPr(prhs0);y=mxGetPr(plhs0); timestwo(y,x);在 matlab 。

6、的命令提示符下,输入 mex Test.cpp 进行编译, 能够得到 Test.dll 文件。将 Test.dll 拷入到工作目录,在matlab 中直接调用 Test()函数即可,如 Test(3),结果为 6。这实际上是一种在 matlab 中的混合编程,能够用 c 或c+来书写。“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful life, learning is an ete。

7、rnal theme. As a professional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值