C语言调用matlab函数 (c语言调用命令行)

本文目录导航:

C语言调用matlab函数

matlab中调用c语言函数:#include mex.h//头文件必须包含 mexSimpleDemo(double *y,double a,double b); //C语言算法程序声明,在最后调用时,第一// 个参数是返回结果//c语言到matlab变换,以mexFunction命名void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){double *y;double m,n;//获取输入变量的数值大小m=mxGetScalar(prhs[0]);n=mxGetScalar(prhs[1]);//获取输出变量的指针plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL);y=mxGetPr(plhs[0]); //调用子函数mexSimpleDemo(y,m,n);}//C语言函数double mexSimpleDemo(double *y,double a,double b){return *y=(a>b)?a:b;}

C语言调用matlab函数 (c语言调用命令行)

linux c++ 怎么 调用自己函数的

实验平台:ubuntu 12.04 + g++4.6 + matlab2012a问题描述:有一个c++程序,和一个matlab函数myFunc.m。

现在要做这件事:1)从中传递2个double类型的数值a和b到myFunc.m中2)myFunc.m中求和(sum = a+b)3)中接收myFunc.m返回的和并输出。

思路:1)设置matlab的编译器,使用gcc编译器。

编译m文件成。

2)编译文件,编译时调用库(添加的路径到编译选项)。

步骤:1)将自己的matlab函数(myFunc.m)编译成动态链接库(1) 设定编译器为gcc,在matlab 命令行依次执行命令mex -setup和mbuild -setup:>> mex -setupOptions files control which compiler to use, the compiler and link command options, and the runtime libraries to link the mex -setup command selects an options file that isplaced in ~//R2012a and used by default for mex. An options file in the current working directory or specified on the command line overrides the default options file in ~//R2012a. To override the default options file, use the mex -f command(see mex -help for more information) options files available for mex are:1: /opt/MATLAB/R2012a/bin/ : Template Options file for building gcc MEX-files 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/ is being copied to ~//R2012a/: cannot create regular file `~//R2012a/: Permission denied**************************************************************************Warning: The MATLAB C and Fortran API has changed to support MATLABvariables with more than 2^32-1 the near futureyou will be required to update your code to utilize the newAPI. You can find more information about this at:Building with the -largeArrayDims option enables the new API. **************************************************************************>> mbuild -setup Options files control which compiler to use, the compiler and link commandoptions, and the runtime libraries to link the mbuild -setup command selects an options file that isplaced in ~//R2012a and used by default for mbuild. An options file in the current working directory or specified on the command line overrides the default options file in ~//R2012a. To override the default options file, use the mbuild -f command (see mbuild -help for more information) options files available for mbuild are:1: /opt/MATLAB/R2012a/bin/ : Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/ is being copied to ~//R2012a/: cannot create regular file `~//R2012a/: Permission denied>>(2) 在matlab中,编写myFunc.m文件内容如下:function [ C ] = myFunc(A, B)C = A+B;end(3) 生成myFunc.m的动态链接库()>> mcc -W cpplib:libmyFunc -T link:lib myFunc.m -cWarning: MATLAB Toolbox Path Cache is out of date and is not being help toolbox_path_cache for more info >> 等待数秒,完成。

可以看到myFunc.m所在的目录下生成了多个文件:$ 命令解释:其中-W是控制编译之后的封装格式;cpplib是指编译成C++的lib;cpplib冒号后面是指编译的库的名字;-T表示目标,link:lib表示要连接到一个库文件的目标,目标的名字即是.m函数的名字。

-c表明需要生成文件,比如本例如果不加-c就不会生成“”。

生成的文件中打开“libmyFunc.h”可以看到一行:extern LIB_libmyFunc_CPP_API void MW_CALL_CONV myFunc(int nargout, mwArray& C, const mwArray& A, const mwArray& B); 这个就是我们的myFunc.c函数待会儿在c++中调用时的接口。

有4个参数,第一个是参数个数,第二个是用来接收函数返回值的,后面2个是从c++中传递进来的变量。

我们还会用到“libmyFunc.h”中的另外2个函数:libmyFuncInitialize()初始化,和注销libmyFuncTerminate()。

2)编写,代码如下:#include <iostream>#include mclmcr.h#include matrix.h#include mclcppclass.h#include libmyFunc.h#include namespace std;int main() {// initialize lib,这里必须做初始化!if( !libmyFuncInitialize()){std::cout << Could not initialize libmyFunc! << std::endl;return -1;}// 用户输入2个数值double a, b;cout<<Please input 2 numbers <a b> and then press enter: <<endl;cin >> a;cin >> b;double c; //used to receive the result// 为变量分配内存空间, maltab只有一种变量,就是矩阵,为了和c++变量接轨,设置成1*1的矩阵mwArray mwA(1, 1, mxDOUBLE_CLASS); //1,1表示矩阵的大小, mxDOUBLE_CLASS表示变量的精度mwArray mwB(1, 1, mxDOUBLE_CLASS);mwArray mwC(1, 1, mxDOUBLE_CLASS);// 调用类里面的SetData函数给类赋值(&a, 1);(&b, 1);// 调用自己的函数,求和。

myFunc(1, mwC, mwA, mwB);c = (1, 1);cout<<The sum is: <<c<<endl;// 后面是一些终止调用的程序// terminate the liblibmyFuncTerminate();// terminate MCRmclTerminateApplication();return EXIT_SUCCESS;}3)编译函数,调用$ g++ -o main -I. -I/opt/MATLAB/R2012a/extern/include -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 -lmyFunc -lm -lmwmclmcrrt -lmwmclmcr开始编译时也遇到一些问题,主要是链接库路径问题导致的编译错误,详细错误汇总在另篇笔记里(点此查看)。

编译成功后,需要装一下MCR Installer,步骤点此。

运行结果:$ ./main Warning: latest version of matlab app-defaults file not your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 10 100The sum is: 110$ ./main Warning: latest version of matlab app-defaults file not your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 1 1The sum is: 2源代码及编译过程中的所有文件已打包,点击这里可以下载。

matlab调用c或c++源程序,能够头文件和源文件分开写,然后mex文件调用头文件

我转过来的 写的很清楚 你参考下吧,把你的pthis dc等参数对照一下就可以了如果我有一个用C语言写的函数,实现了一个功能,如一个简单的函数:double add(double x, double y) { return x + y;}现在我想要在Matlab中使用它,比如输入:>> a = add(1.1, 2.2) 3.3000要得出以上的结果,那应该怎样做呢?解决方法之一是要通过使用MEX文件,MEX文件使得调用C函数和调用Matlab的内置函数一样方便。

MEX文件是由原C代码加上MEX文件专用的接口函数后编译而成的。

可以这样理解,MEX文件实现了一种接口,它把在Matlab中调用函数时输入的自变量通过特定的接口调入了C函数,得出的结果再通过该接口调回Matlab。

该特定接口的操作,包含在mexFunction这个函数中,由使用者具体设定。

所以现在我们要写一个包含add和mexFunction的C文件,Matlab调用函数,把函数中的自变量(如上例中的1.1和2.2)传给mexFunction的一个参数,mexFunction把该值传给add,把得出的结果传回给mexFunction的另一个参数,Matlab通过该参数来给出在Matlab语句中调用函数时的输出值(如上例中的a)。

比如该C文件已写好,名为add.c。

那么在Matlab中,输入:>> mex add.c就能把add.c编译为MEX文件(编译器的设置使用指令mex -setup),在Windows中,MEX文件类型为mexw32,即现在我们得出32文件。

现在,我们就可以像调用M函数那样调用MEX文件,如上面说到的例子。

所以,通过MEX文件,使用C函数就和使用M函数是一样的了。

我们现在来说mexFunction怎样写。

mexFunction的定义为:void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {}可以看到,mexFunction是没返回值的,它不是通过返回值把结果传回Matlab的,而是通过对参数plhs的赋值。

mexFunction的四个参数皆是说明Matlab调用MEX文件时的具体信息,如这样调用函数时:>> b = 1.1; c = 2.2;>> a = add(b, c)mexFunction四个参数的意思为:nlhs = 1,说明调用语句左手面(lhs-left hand side)有一个变量,即a。

nrhs = 2,说明调用语句右手面(rhs-right hand side)有两个自变量,即b和c。

plhs是一个数组,其内容为指针,该指针指向数据类型mxArray。

因为现在左手面只有一个变量,即该数组只有一个指针,plhs[0]指向的结果会赋值给a。

prhs和plhs类似,因为右手面有两个自变量,即该数组有两个指针,prhs[0]指向了b,prhs[1]指向了c。

要注意prhs是const的指针数组,即不能改变其指向内容。

因为Matlab最基本的单元为array,无论是什么类型也好,如有double array、 cell array、 struct array……所以a,b,c都是array,b = 1.1便是一个1x1的double array。

而在C语言中,Matlab的array使用mxArray类型来表示。

所以就不难明白为什么plhs和prhs都是指向mxArray类型的指针数组。

完整的add.c如下:// add.c#include mex.h // 使用MEX文件必须包含的头文件// 执行具体工作的C函数double add(double x, double y) { return x + y;}// MEX文件接口函数void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *a; double b, c; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); a = mxGetPr(plhs[0]); b = *(mxGetPr(prhs[0])); c = *(mxGetPr(prhs[1])); *a = add(b, c);}mexFunction的内容是什么意思呢?我们知道,如果这样调用函数时:>> output = add(1.1, 2.2);在未涉及具体的计算时,output的值是未知的,是未赋值的。

所以在具体的程序中,我们建立一个1x1的实double矩阵(使用mxCreateDoubleMatrix函数,其返回指向刚建立的mxArray的指针),然后令plhs[0]指向它。

接着令指针a指向plhs[0]所指向的mxArray的第一个元素(使用mxGetPr函数,返回指向mxArray的首元素的指针)。

同样地,我们把prhs[0]和prhs[1]所指向的元素(即1.1和2.2)取出来赋给b和c。

于是我们可以把b和c作自变量传给函数add,得出给果赋给指针a所指向的mxArray中的元素。

因为a是指向plhs[0]所指向的mxArray的元素,所以最后作输出时,plhs[0]所指向的mxArray赋值给output,则output便是已计算好的结果了。

上面说的一大堆指向这指向那,什么mxArray,初学者肯定都会被弄到头晕眼花了。

很抱歉,要搞清楚这些乱糟糟的关系,只有多看多练。

实际上mexFunction是没有这么简单的,我们要对用户的输入自变量的个数和类型进行测试,以确保输入正确。

如在add函数的例子中,用户输入char array便是一种错误了。

从上面的讲述中我们总结出,MEX文件实现了一种接口,把C语言中的计算结果适当地返回给Matlab罢了。

当我们已经有用C编写的大型程序时,大可不必在Matlab里重写,只写个接口,做成MEX文件就成了。

另外,在Matlab程序中的部份计算瓶颈(如循环),可通过MEX文件用C语言实现,以提高计算速度。

你可能想看:
« 上一篇
下一篇 »

相关推荐

FreeBasic:强大免费开源的64位basic编译器及应用优势

2025年05月10日

82阅读

脊柱侧弯矫正方法大盘点:运动康复、支具矫正等多种方式

2025年05月09日

88阅读

如何在winForm标题栏去掉标题文字且任务栏图标正常显示

2025年05月09日

86阅读

十一黄金周促销广告词大全,国庆购物狂欢等你来

2025年05月09日

78阅读

关闭快速编辑模式及插入模式,设置控制台指定文字颜色举例

2025年05月09日

63阅读

黄金搭档低糖高钙饮品包装及成分功效全解析,适合人群要知晓

2025年05月09日

91阅读