Mex函数与C++数据矩阵交互方法

 1 #include "mex.h"
 2 #include <iostream>
 3 #include <windows.h>
 4 #include "math.h"
 5 #include <opencv2\opencv.hpp>
 6 using namespace cv;
 7 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
 8 {
 9     if(nrhs==1)
10     {
11         double *indata;
12         double *outdata;
13         int M,N;
14         int i,j;
15         indata = mxGetPr(prhs[0]);
16         M = mxGetM(prhs[0]);
17         N =mxGetN(prhs[0]);
18         for(i =0; i<M; i++)
19         {
20             for(j =0; j<N; j++)
21             {
22                 mexPrintf("%4.3f  \n",indata[j*M+i]);
23             }
24         }
25         plhs[0] = mxCreateDoubleMatrix(M,N,mxREAL);
26         outdata = mxGetPr(plhs[0]);
27         for(i =0; i<M; i++)
28         {
29             for(j =0; j<N; j++)
30             {
31                 /*outdata[j*M+i]=indata[(N-1-j)*M+i];*/
32                 outdata[j*M+i]=indata[j*M+i];
33             }
34         }
35     }
36 }
View Code

简单的实现matlab与C++之间的矩阵输入输出。

posted @ 2014-10-30 14:55  峰回璐转  阅读(1498)  评论(0编辑  收藏  举报