【转】matlab 生成调用c/c++ 代码 mex 简单实用——修改部分

原文地址:https://blog.csdn.net/xiaoyanghijk/article/details/52370459

目标:在matlab中送入两张图像(矩阵),对图像进行处理,输出为一副图像(矩阵)。

更为详细的用matlab调用c/c++对数组进行处理的例子,

请见下一个博客:  http://blog.csdn.net/xiaoyanghijk/article/details/52387089 

更为详细的代码下载:http://download.csdn.net/detail/xiaoyanghijk/9618136

要熟悉下这个函数的意思:

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

{

}

Nlhs :输入参数个数

Nrhs:输出参数个数

Plhs[ ]:输入参数列表

Prhs[ ] :输出参数列表

开始动手:

第一步:在matalb 当前目录下放人fun_name.cpp文件。即以函数名命名的文件——

如定义hello.cpp文件在当前路径下:

     

#include "mex.h" 

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

{

int i; 

i=mxGetScalar(prhs[0]); 

if(i==1) 

  mexPrintf("hello,world!\n"); 

else 

  mexPrintf("大家好!\n"); 

}

第二步: 在matalb 窗口执行语句:mex  fun_name.cpp——

这里是输入:mex hello.cpp,会生成一个 hello.mexw64 。一定要先有这个文件。

第三步:执行——

命令行输入:

hello(1)

回车:
hello,world!

实例2:

void  mexFunction(int  nlhs,  mxArray * plhs[], int nrhs,  const mxArray *prhs[])
{
double *data; 
int M,N; 
int i,j; 
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"); 
  } 
  }

按照上面第二步编译完成后,命令行输入:

>>a = [1 2 3; 4 5 6 ;7 8 9; 10 11 12]

回车:

a =

     1     2     3
     4     5     6
     7     8     9
    10    11    12

再输入:
>> hello(a)
1.000  2.000  3.000  /n4.000  5.000  6.000  /n7.000  8.000  9.000  /n10.000  11.000  12.000  /n>> 

更为详细的用matlab调用c/c++对数组进行处理的例子,请见下一个博客:  

http://blog.csdn.net/xiaoyanghijk/article/details/52387089 

详细代码下载:http://download.csdn.net/detail/xiaoyanghijk/9618136

参考链接:

1)http://blog.csdn.net/gxiaob/article/details/8679770

2)http://blog.csdn.net/jtop0/article/details/7657227

3) http://blog.sciencenet.cn/home.php?mod=space&uid=43777&do=blog&id=320103 

4)http://mcximing.github.io/2014/12/12/mex/ matlab 与opencv 混编

猜你喜欢

转载自blog.csdn.net/weixin_40194697/article/details/81171119
Mex