用mexmatlab 调用mex文件cpp,为什么出现没有cv.h文件

由于看的代码里面以及一些工程需要涉及一些Matlab 混合编程,一直想看一下,首先一个就是使用C编写代码,编译之后由Matlab调用,这需要用到Mex函数,mex程序就是根据一定的接口规范(mtlab提出的)编写的一个dll,Mex文件既可以用c,也可以用fortran来编。本文介绍的是用C编写的。
这样做,若是代码中有循环的话,使用matlab的话需要循环解释多次,而使用C语言编译成dll之后,首先可以将循环体放入二进制程序中,利用matlab加快运算,而不是循环解释执行matlab代码。还有就是使用C可以加快代码的开发效率。
设置编译器路径
在Matlab 命令窗口键入& & mex -setup,下面只要根据提示一步步设置就可以了。
为了测试你的路径设置正确与否,把下面的程序存为hello.c。
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexPrintf(&hello,world!\n&);
假设你把hello.c放在了C:\TEST\下,在Matlab里用CD C:\TEST\ 将当前目录改为C:\TEST\(注意,仅将C:\TEST\加入搜索路径是没有用的)。现在敲:
mex hello.c
&& 如果一切顺利,编译应该在出现编译器提示信息后正常退出。如果你已将C:\TEST\加入了搜索路径,现在键入hello,程序会在屏幕上打出一行:hello,world!
看看C\TEST\目录下,你会发现多了一个文件:HELLO.DLL。说明Mex函数已经完成,编译成功
接口函数规范mexFunction介绍
void&mexFunction(int&nlhs,&mxArray&*plhs[],&int&nrhs,&const&mxArray&*prhs[])
nlhs:输出参数数目&
plhs:指向输出参数的指针&
nrhs:输入参数数目&
例如,使用
[a,b]=test(c,d,e)
调用mex函数test时,传给test的这三个参数分别是&prhs[0]=c&,prhs[1]=d&,prhs[2]=e&
当函数返回时,将会把你放在plhs[0],plhs[1]里的地址赋给a和b,达到返回数据的目的。&&
细心的你也许已经注意到,prhs[i]和plhs[i]都是指向类型mxArray类型数据的指针。&这个类型是在mex.h中定义的,事实上,在Matlab里大多数数据都是以这种类型存在。当然还有其他的数据类型,可以参考Apiguide.pdf里的介绍。
为了让大家能更直观地了解参数传递的过程,我们把hello.c改写一下,使它能根据输&入参数的变化给出不同的屏幕输出:
//hello.c 2.0
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
i=mxGetScalar(prhs[0]);
mexPrintf(&hello,world!\n&);
mexPrintf(&大家好!\n&);
将这个程序编译通过后,执行hello(1),屏幕上会打出:&
&&&&&&&&&&hello,world!&
而hello(0)将会得到:&
&&&&&&&&&&&大家好!&
现在,程序hello已经可以根据输入参数来给出相应的屏幕输出。在这个程序里,除了用到了屏幕输出函数mexPrintf(用法跟c里的printf函数几乎完全一样)外,还用到了一个函数:mxGetScalar,调用方式如下:&
i=mxGetScalar(prhs[0]);&
&Scalar&就是标量的意思。在Matlab里数据都是以数组的形式存在的,mxGetScalar的作用就是把通过prhs[0]传递进来的mxArray类型的指针指向的数据(标量)赋给C程序里的变量。这个变量本来应该是double类型的,通过强制类型转换赋给了整形变量i。既然有标量,显然还应该有矢量,否则矩阵就没法传了。看下面的程序:&
//hello.c 2.1
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
i=mxGetPr(prhs[0]); //prhs是一个指针数组(每一个是一个指针),所以返回一个指针
if(i[0]==1)
mexPrintf(&hello,world!\n&);
mexPrintf(&大家好!\n&);
这样,就通过mxGetPr函数从指向mxArray类型数据的prhs[0]获得了指向double类型的指针。
但是,还有个问题,如果输入的不是单个的数据,而是向量或矩阵,那该怎么处理呢&?通过mxGetPr只能得到指向这个矩阵的指针,如果我们不知道这个矩阵的确切大小,就&没法对它进行计算。&
为了解决这个问题,Matlab提供了两个函数mxGetM和mxGetN来获得传进来参数的行数&和列数。下面例程的功能很简单,就是获得输入的矩阵,把它在屏幕上显示出来:&
//show.c 1.0
#include &mex.h&
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
data=mxGetPr(prhs[0]); //获得指向矩阵的指针
M=mxGetM(prhs[0]); //获得矩阵的行数
N=mxGetN(prhs[0]); //获得矩阵的列数
for(i=0;i&M;i++)
for(j=0;j&N;j++)
mexPrintf(&%4.3f
&,data[j*M+i]);
mexPrintf(&\n&);
编译完成后,用下面的命令测试一下:&
b=[a;a+1];
还有就是若是数据不是二维的,可以通过下面的函数获得数据的维度个数(向量:1,矩阵:2,...)以及维度数组。
需要注意的是,在Matlab里,矩阵第一行是从1开始的,而在C语言中,第一行的序数为零,Matlab里的矩阵元素b(i,j)在传递到C中的一维数组data后对应于data[j*M+i]&。&输入数据是在函数调用之前已经在Matlab里申请了内存的,由于mex函数与Matlab共用同一个地址空间,因而在prhs[]里传递指针就可以达到参数传递的目的。但是,输出参数却需要在mex函数内申请到内存空间,才能将指针放在plhs[]中传递出去。由于返回指针类型必须是mxArray,所以Matlab专门提供了一个函数:mxCreateDoubleMatrix来实现内存的申请,函数原型如下:&
&&&mxArray&*mxCreateDoubleMatrix(int&m,&int&n,&mxComplexity&ComplexFlag)&
&&&m:待申请矩阵的行数&
&&&n:待申请矩阵的列数&
为矩阵申请内存后,得到的是mxArray类型的指针,就可以放在plhs[]里传递回去了。但是对这个新矩阵的处理,却要在函数内完成,这时就需要用到前面介绍的mxGetPr。使用&mxGetPr获得指向这个矩阵中数据区的指针(double类型)后,就可以对这个矩阵进行各种操作和运算了。下面的程序是在上面的show.c的基础上稍作改变得到的,功能是将输出处理后的元素:
//reverse.c 1.0
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
double *inD
double *outD
inData=mxGetPr(prhs[0]);
M=mxGetM(prhs[0]);
N=mxGetN(prhs[0]);
plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL);
outData=mxGetPr(plhs[0]);
for(i=0;i&M;i++)
for(j=0;j&N;j++)
outData[j*M+i]=inData[(N-1-j)*M+i];
当然,Matlab里使用到的并不是只有double类型这一种矩阵,还有字符串类型、稀疏矩阵、结构类型矩阵等等,并提供了相应的处理函数。本文用到编制mex程序中最经常遇到的一些函数,其余的详细情况清参考Apiref.pdf。&
另外,若是不是输出元素,申请内存之后要记得释放:
mwSize&*subS&
subScript&=&(mwSize&*)mxCalloc(&numOfDim,&sizeof(&mwSize&)&);&
mxFree(&subScript&);&
通过前面两部分的介绍,大家对参数的输入和输出方法应该有了基本的了解。具备了这些知识,就能够满足一般的编程需要了。但这些程序还有些小的缺陷,以前面介绍的re由于前面的例程中没有对输入、输出参数的数目及类型进行检查,导致程序的容错性很差,以下程序则容错性较好
#include &mex.h&
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
double *inD
double *outD
//异常处理
//异常处理
if(nrhs!=1)
mexErrMsgTxt(&USAGE: b=reverse(a)\n&);
if(!mxIsDouble(prhs[0]))
mexErrMsgTxt(&the Input Matrix must be double!\n&);
inData=mxGetPr(prhs[0]);
M=mxGetM(prhs[0]);
N=mxGetN(prhs[0]);
plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL);
outData=mxGetPr(plhs[0]);
for(i=0;i&M;i++)
for(j=0;j&N;j++)
outData[j*M+i]=inData[(N-1-j)*M+i];
在上面的异常处理中,使用了两个新的函数:mexErrMsgTxt和mxIsDouble。MexErrMsgTxt在给出出错提示的同时退出当前程序的运行。MxIsDouble则用于判断mxArray中的数据是否double类型。当然Matlab还提供了许多用于判断其他数据类型的函数,这里不加详述。&
需要说明的是,Matlab提供的API中,函数前缀有mex-和mx-两种。带mx-前缀的大多是对mxArray数据进行操作的函数,如mxIsDouble,mxCreateDoubleMatrix等等。而带mx前缀的则大多是与Matlab环境进行交互的函数,如mexPrintf,mxErrMsgTxt等等。了解了这一点,对在Apiref.pdf中查找所需的函数很有帮助。
另外,需要注意的是,当代码的后缀名为.c的时候,需要一次性把变量声明完,且放在代码的最前面,后缀为.cpp不会出现这个问题。
常用的Mex函数一览表
MX Matrix Library
Type for index values
Pointer type for platform
Signed integer type for size values
Type for size values
Field to structure array
Type for MATLAB array
Convert array to string
Check assertion value for debugging purposes
Check assertion value without printing assertion text
Offset from first element to desired element
Allocate dynamic memory for array using MATLAB memory manager
Type for string array
Enumerated value identifying class of array
Identifier corresponding to class
Flag specifying whether array has imaginary components
CHARACTER values from Fortran array to pointer array
COMPLEX*16 values from Fortran array to pointer array
COMPLEX*8 values from Fortran array to pointer array
INTEGER*1 values from Fortran array to pointer array
INTEGER*2 values from Fortran array to pointer array
INTEGER*4 values from Fortran array to pointer array
CHARACTER values from pointer array to Fortran array
COMPLEX*16 values from pointer array to Fortran array
COMPLEX*8 values from pointer array to Fortran array
INTEGER*1 values from pointer array to Fortran array
INTEGER*2 values from pointer array to Fortran array
INTEGER*4 values from pointer array to Fortran array
Pointer values from pointer array to Fortran array
REAL*4 values from pointer array to Fortran array
REAL*8 values from pointer array to Fortran array
REAL*4 values from Fortran array to pointer array
REAL*8 values from Fortran array to pointer array
Unpopulated N-D cell array
Unpopulated 2-D cell array
Unpopulated N-D string array
Create populated 2-D string array
2-D, double-precision, floating-point array initialized to 0
Scalar, double-precision array initialized to specified value
N-D logical array initialized to false
2-D, logical array initialized to false
Scalar, logical array
Unpopulated N-D numeric array
Numeric matrix initialized to 0
2-D unpopulated sparse array
Unpopulated 2-D, sparse, logical array
Create 1-by-N array initialized to specified string
Unpopulated N-D structure array
Unpopulated 2-D structure array
Free dynamic memory allocated by MXCREATE* functions
Make deep copy of array
Free dynamic memory allocated by MXCALLOC, MXMALLOC, or MXREALLOC functions
Contents of array cell
Pointer to character array data
Class of array
Class of array as string
Pointer to real data
Pointer to dimensions array
Number of bytes required to store each data element
Value of EPS
Field value, given field name and index, into structure array
Field value, given field number and index, into structure array
Field name, given field number, in structure array
Field number, given field name, in structure array
Pointer to imaginary data of array
Value of infinity
Sparse matrix IR array
Sparse matrix JC array
Pointer to logical array data
Number of rows in array
Number of columns in array
Value of NaN (Not-a-Number)
Number of dimensions in array
Number of elements in array
Number of fields in structure array
Number of elements in IR, PR, and PI arrays
Imaginary data elements in array of type DOUBLE
Real data elements in array of type DOUBLE
Value of public property of MATLAB object
Real component of first data element in array
String array to C-style string
Determine whether input is cell array
Determine whether input is string array
Determine whether array is member of specified class
Determine whether data is complex
Determine whether mxArray represents data as double-precision, floating-point numbers
Determine whether array is empty
Determine whether input is finite
Determine whether array was copied from MATLAB global workspace
Determine whether input is infinite
Determine whether array represents data as signed 16-bit integers
Determine whether array represents data as signed 32-bit integers
Determine whether array represents data as signed 64-bit integers
Determine whether array represents data as signed 8-bit integers
Determine whether array is of type mxLogical
Determine whether scalar array is of type mxLogical
Determine whether scalar array of type mxLogical is true
Determine whether input is NaN (Not-a-Number)
Determine whether array is numeric
Determine whether array represents data as single-precision, floating-point numbers
Determine whether input is sparse array
Determine whether input is structure array
Determine whether array represents data as unsigned 16-bit integers
Determine whether array represents data as unsigned 32-bit integers
Determine whether array represents data as unsigned 64-bit integers
Determine whether array represents data as unsigned 8-bit integers
Type for logical array
Allocate dynamic memory using MATLAB memory manager
Reallocate dynamic memory using MATLAB memory manager
Remove field from structure array
Value of one cell of array
Convert structure array to MATLAB object array
Set pointer to data
Modify number of dimensions and size of each dimension
Set structure array field, given structure field name and array index
Set structure array field, given field number and index
Imaginary data pointer for array
IR array of sparse array
JC array of sparse array
Number of rows in array
Set number of columns in array
Set storage space for nonzero elements
Set new imaginary data for array
Set new real data for array
Set value of public property of MATLAB object
MEX Library
Register function to call when MEX-function cleared or MATLAB software terminates
Call MATLAB function, user-defined function, or MEX-file
Call MATLAB function, user-defined function, or MEX-file and capture error information
Display error message with identifier and return to MATLAB prompt
Display error message and return to MATLAB prompt
Execute MATLAB command in caller workspace
Execute MATLAB command in caller workspace and capture error information
Entry point to C/C++ or Fortran MEX-file
Name of current MEX-function
Value of specified Handle Graphics property
Copy of variable from specified workspace
Read-only pointer to variable from another workspace
Determine whether variable has global scope
Determine whether MEX-file is locked
Prevent clearing MEX-file from memory
Make array persist after MEX-file completes
Make memory allocated by MATLAB software persist after MEX-function completes
ANSI C PRINTF-style output routine
Array from MEX-function into specified workspace
Set value of specified Handle Graphics property
Control response of MEXCALLMATLAB to errors
Allow clearing MEX-file from memory
Warning message with identifier
Warning message
/blog-885.html
http://blog.csdn.net/raodotcong/article/details/6295859
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:75781次
积分:1722
积分:1722
排名:第12957名
原创:95篇
转载:11篇
评论:44条使用matlab进行mex编译时的路径问题mexopts
matlab和vs 进行混合编程时总需要使用matlab编译mexFunction.cpp文件。这些文件免不了使用include下的*.h和lib下的*.lib文件。举
matlab和vs 进行混合编程时总需要使用matlab编译mexFunction.cpp文件。这些文件免不了使用include下的*.h和lib下的*.lib文件。举例说明,这次我的cpp中用到了opencv/cv.h。在matlab\bin\win64\mexopts文件夹下有一个msvsopts100.bat的批处理文件。
用notepad 将这个文件打开在&include=& 和 &lib=& 后面添加需要使用的路径并加分号&;&
设置好之后保存。然后在matlab 中 输入mex mexFunction.cpp。但是还是会出现 cannot open file &opencv/cv.h&。这是为什么呢?
仔细观察发现mscvopts100.bat 中第6行中有这样一句话rem C++keyName: Microsoft Visual C++ 2010。这句话证明mscvopts100.bat ,,在mex -setup的过程中修改过。因此可以猜测需要再次进行mex -setup 才能使这些路径生效。经过试验,确实如此。困扰了我两周。希望能给大家省时间。
Matlab与C/C++联合编程之从Matlab调用C/C++代码
二分类SVM方法Matlab实现
Matlab中的取整函数fix, floor, ceil与round
Matlab编译cuda的.cu文件
本文永久更新链接地址:
你最喜欢的用matlab的 mex编译C文件出现如下错误,请问下有没有知道为什么_百度知道
用matlab的 mex编译C文件出现如下错误,请问下有没有知道为什么
}FirstP FirstP' ;Hello World: '
k= i+j;mex! \
j=2, mxArray *plhs[];
mexPrintf(&quot, const mxArray *prhs[]){
int i,n&quot.h&: error C2143;k&#39.c(9) : error C2065: syntax error : missing '
k=0;void mexFunction(int nlhs: undeclared identifier 第8行为int k.c(8) ;
i=1; before 'type').c FirstProgram#include&quot
提问者采纳
为什么要把变量声明都放在一起呢
C的规定,C不是C++
提问者评价
非常感谢,平时用惯了C++,C的规定却忘了
其他类似问题
为您推荐:
matlab的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁已有天涯账号?
这里是所提的问题,您需要登录才能参与回答。
"天涯问答"是天涯社区旗下的问题分享平台。在这里您可以提问,回答感兴趣的问题,分享知识和经历,无论您在何时何地上线都可以访问,此平台完全免费,而且注册非常简单。
用matlab和c写程序,include的mex.h在哪里?
用matlab和c写程序,include的mex.h在哪里?
09-08-09 & 发布
span name=whlm id=whlm请看这个帖子【mcc、mbuild和mex命令详解】a href=&&(1)创建MEX文件&&mcc –x filename (注意这个命令在2008a中已经去掉了)将M文件filename.m翻译成C代码,并生成一个可被Matlab直接调用的C的MEX。(2)创建simulink S函数&&mcc –s filename将M文件filename.m翻译成C代码,并生成一个相应的simulink S函数,该函数的输入输出变量的大小可动态改变。(3)创建可独立执行的C程序&&mcc –m filename将M文件filename.m翻译成C代码,生成的可执行文件能独立于Matlab运行环境。(4)创建可独立运行的C++程序&&mcc –p filename将M文件filename.m翻译成C++代码,生成的可执行文件能独立于Matlab运行环境。(5)创建可独立运行的C图形库函数&&mcc –B sgl filename如果filename.m中包含了对Matlab图形处理函数的调用,上述命令,将filename转换成为C语言,并生成一个能独立于Matlab运行环境的可执行程序。(6) 创建可独立运行的C++图形库函数&&mcc –B sgl cpp filename如果filename.m中包含了对Matlab图形处理函数的调用,上述命令,将filename转换成为C++语言,并生成一个能独立于Matlab运行环境的可执行程序。(7)创建C函数库&&mcc –m –W lib:libfoo –T link:libfoo.m创建一个C函数库/span
请登录后再发表评论!Win7上Matlab中使用Visual Studio 2010编译Opencv的Mex文件 - 编程当前位置:& &&&Win7上Matlab中使用Visual Studio 2010编译Opencv的Win7上Matlab中使用Visual Studio 2010编译Opencv的Mex文件&&网友分享于:&&浏览:199次Win7下Matlab中使用Visual Studio 2010编译Opencv的Mex文件编译命令:
mex testmex.cpp -I&C:\opencv24\opencv\build\include& -I&C:\opencv24\opencv\build\include\opencv& -L&C:\opencv24\opencv\build\x64\vc10\lib& -lopencv_core241 -lopencv_highgui241 -lopencv_imgproc241
这里关键是正确指定库文件查找路径
testmex.cpp内容
//file: testmex.cpp
//编译命令:mex testmex.cpp -I&C:\opencv24\opencv\build\include& -I&C:\opencv24\opencv\build\include\opencv& -L&C:\opencv24\opencv\build\x64\vc10\lib& -lopencv_core241 -lopencv_highgui241 -lopencv_imgproc241
#include &iostream&
#include &mex.h&
#include &opencv2/opencv.hpp&
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexPrintf(&come1\n&);
Mat src = cv::imread(&d:\\bird.jpg&, CV_LOAD_IMAGE_GRAYSCALE),
threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
imshow(&Source&, src);
cv::imshow(&Result&, dst);
cv::waitKey(0);
cvDestroyAllWindows();
mexPrintf(&over\n&);
catch(const cv::Exception& ex)
char err[512];
sprintf(err, &Error:%s&, ex.what());
mexPrintf(err);
mexPrintf(&ok&);
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有

我要回帖

更多关于 matlab调用mex 的文章

 

随机推荐