如何使用Microsoft Visual C++ 2010 通过 MATLAB 7.10 (R2010a) 创建 MEX

How can I use Microsoft Visual C++ 2010 to create MEX files with MATLAB 7.10 (R2010a)?

本文关键字:R2010a MEX 创建 MATLAB Microsoft 何使用 Visual C++ 通过 2010      更新时间:2023-10-16

以下问题:
如何设置 LIBSVM Matlab 接口?
为什么在 r2010a 中键入 mex -setup 时我的 Windows 7 上"没有编译器"?
我遇到了链接:
如何使用 Microsoft Visual C++ 2010 创建带有 MATLAB 7.10 (R2010a) 的 MEX 文件?
页面中说补丁将支持这些组合:

• Visual C++ 2010 Professional and 64-bit MATLAB 7.10 (R2010a)
• Visual C++ 2010 Professional and 32-bit MATLAB 7.10 (R2010a)
• Visual C++ 2010 Express (Windows SDK 7.1 also required) and 64-bit MATLAB 7.10 (R2010a)
• Visual C++ 2010 Express and 32-bit MATLAB 7.10 (R2010a)

但是我的笔记本上Visual C++ 2010 Ultimate安装了。如何了解修补程序是否支持此组合?

• Visual C++ 2010 Ultimate and 64-bit MATLAB 7.10 (R2010a)

是的,当然。

首先,您应该将包含文件夹,libs文件夹添加到VS include&libs中。

其次,你通过C++实现你的 mex 文件,你应该先将 mex.h 文件包含在你的源代码中,然后 mexFunction 必须根据 mex 的规则提供。如下:

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

第三,您应该将库添加到其他依赖项中,例如libmx.lib,libmat.lib,libmex.lib等。

PS:当你尝试实现一个matlab mex项目时,你应该使用VS创建一个dll项目,事实上,mex文件(mexw32和mexw64)是一个特殊的dll文件。所以VS生成的文件是一个dll文件,如果你想让VS生成一个mex文件,你可以通过修改VS项目的配置项来更改文件名,或者在它们生成后重命名文件。

在这里,我只向您展示一些代码:

#include "Rectification.h"
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, 
             const mxArray *prhs[] )
{
    if (nrhs != 4)
    {
        mexErrMsgTxt("You need input just 4 parameters!");
    }
    int m1 = mxGetM(prhs[0]);
    int n1 = mxGetN(prhs[0]);
    int m2 = mxGetM(prhs[1]);
    int n2 = mxGetN(prhs[1]);
    int m3 = mxGetM(prhs[2]);
    int n3 = mxGetN(prhs[2]);
    int m4 = mxGetM(prhs[3]);
    int n4 = mxGetN(prhs[3]);
    double* temp1 = mxGetPr(prhs[0]);
    double* temp2 = mxGetPr(prhs[1]);
    double* temp3 = mxGetPr(prhs[2]);
    double* temp4 = mxGetPr(prhs[3]);
    CMatrix size(m1, n1, temp1);
    CMatrix inliers1(m2, n2, temp2);
    CMatrix inliers2(m3, n3, temp3);
    CMatrix fMatrix(m4, n4, temp4);
    CMatrix h1, h2;
    CalH(size, inliers1, inliers2, fMatrix, h1, h2);
    int om1 = h1.GetmRows();
    int on1 = h1.GetmCols();
    plhs[0] = mxCreateDoubleMatrix(om1, on1, mxREAL);
    double* outMat1 = mxGetPr(plhs[0]);
    for (int i = 0; i < om1*on1; i++)
    {
        *(outMat1 + i) = *(h1.GetmData() + i);
    }
    int om2 = h2.GetmRows();
    int on2 = h2.GetmCols();
    plhs[1] = mxCreateDoubleMatrix(om2, on2, mxREAL);
    double* outMat2 = mxGetPr(plhs[1]);
    for (int i = 0; i < om2*on2; i++)
    {
        *(outMat2 + i) = *(h2.GetmData() + i);
    }
}

如果你需要,我可以和你分享我的项目。