一、首先要熟悉matlab所附带的C语言api
1、定义mexFunction函数,mexFunction的定义法唯一: 它只能是如下形式:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
其名称和参数类型不许有任何改变,在mexFunciton函数中可以调用你刚定义好的C++程序。
2、MATFile matOpen(const char *filename, const char mode)——打开/创建
3、MATFile matOpen(const char *filename, const char mode)——打开/创建一个MAT文件;
4、int matClose(MATFile *pMF)——关闭一个MAT文件;
5、mxArray *mxCreateDoubleMatrix(int m, int n, mxComplexity flag) ——创建一个(复)双精度矩阵;
6、`mxArray *mxCreateSparse(int m, int n, int nzmax,mxComplexity flag) ——创建一个稀疏矩阵;
7、mxArray *matGetNextArray(MATFile *pMF)——获得MAT文件里面下一个矩阵;
8、const char *mxGetName(const mxArray *pa)——获得矩阵pa的名称;
9、void mxSetName(mxArray *pa,const char *s)——为矩阵pa设置一个名称;
10、int mxGetM(const mxArray *pa)——获得矩阵pa的总行数;
11、int mxGetN(const mxArray *pa)——获得矩阵pa的总列数;
12、double *mxGetPr(const mxArray *pa)——获得矩阵pa的pr指针;
13、int *mxGetIr(const mxArray *pa)——获得稀疏矩阵pa的ir指针;
14、int *mxGetJc(const mxArray *pa)——获得稀疏矩阵pa的jc指针;
15、int matPutArray(MATFile * pMF, const mxArray * pA) ——把矩阵pA存储入MAT文件pMAF;
16、void mxDestroyArray(mxArray *pa)——释放矩阵pa(把它从内存中撤销);
二、例子(用C++模拟matlab矩阵相加add.cpp)
#include "mex.h" #include<stdlib.h> #include <string.h> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *inData1, *inData2, *outData; int inM1, inN1, inM2, inN2, outM, outN, i, j; //检查输入参数矩阵的个数 if (nrhs != 2) mexErrMsgTxt("输入参数必须为2!"); else if (nlhs > 2) mexErrMsgTxt("Too many output arguments"); //获取输入矩阵的行列 inM1 = mxGetM(prhs[0]); inN1 = mxGetN(prhs[0]); inM2 = mxGetM(prhs[1]); inN2 = mxGetN(prhs[1]); outM = (inM1 > inM2) ? inM1 : inM2; outN = (inN1 > inN2) ? inN1 : inN2; mexPrintf("输出行数为:%d\n",outM); mexPrintf("输出列数为:%d\n",outN); //创建输出矩阵 plhs[0] = mxCreateDoubleMatrix(outM, outN, mxREAL); //获得输入,输出的头指针 inData1 = mxGetPr(prhs[0]); inData2 = mxGetPr(prhs[1]); outData = mxGetPr(plhs[0]); //创建二维数组 double **temp1, **temp2,**answer1,**answer2; temp1 = (double **)malloc(sizeof(double *) * inM1); for (i = 0; i < inM1; i++) temp1[i] = (double *)malloc(sizeof(double) * inN1); temp2 = (double **)malloc(sizeof(double *) * inM2); for (i = 0; i < inM2; i++) temp2[i] = (double *)malloc(sizeof(double) * inN2); answer1 = (double **)malloc(sizeof(double *) * outM); for (i = 0; i < outM; i++) answer1[i] = (double *)malloc(sizeof(double) * outN); answer2 = (double **)malloc(sizeof(double *) * outM); for (i = 0; i < outM; i++) answer2[i] = (double *)malloc(sizeof(double) * outN); //标准answer1初赋值为0 for ( i = 0; i < outM; i++) for (int j = 0; j < outN; j++) answer1[i][j]=0; for ( i = 0; i < outM; i++) for (int j = 0; j < outN; j++) answer2[i][j]=0; //temp一维变二维 for ( i = 0; i < inM1; i++) for (int j = 0; j < inN1; j++) temp1[i][j] = inData1[j*inM1 + i]; //temp2一维变二维 for (i = 0; i < inM2; i++) for ( j = 0; j < inN2; j++) temp2[i][j] = inData2[j*inM2 +i]; //temp1二维扩展至标准 { for ( i = 0; i < inM1; i++) for ( j = 0; j <inN1; j++) answer1[i][j] = temp1[i][j]; } //temp2二维扩展至标准 { for ( i = 0; i < inM2; i++) for ( j = 0; j <inN2; j++) answer2[i][j] = temp2[i][j]; } //标准格式相加,结果输入到answer1 for (i = 0; i < outM; i++) { for (j = 0; j < outN; j++) answer1[i][j]= answer1[i][j]+ answer2[i][j]; } mexPrintf("\n"); //释放小数组temp1 for ( i = 0; i < inM1; i++) { free(temp1[i]); } free(temp1); //释放小数组temp2 for ( i = 0; i < inM2; i++) { free(temp2[i]); } free(temp2); //释放大数组answer2 for ( i = 0; i < outM; i++) { free(answer2[i]); } free(answer2); //二维转一维输出!! for (i = 0; i < outM; i++) for (j = 0; j < outN; j++) { outData[j*outM+i] =answer1[i][j]; } // 释放大数组answer1 for ( i = 0; i < outM; i++) { free(answer1[i]); } free(answer1); }
三、编译
将add.c拷贝至Matlab当前目录,执行mex add.cpp,生成add.mexw64,该文件实现求和功能。此时便可在Matlab中调用该函数
本文出自 “大涛吃汤包” 博客,谢绝转载!
原文:http://kenneth123.blog.51cto.com/3512478/1615067