mexfunction 输入混乱

mexfunction Inputs confusion

本文关键字:混乱 输入 mexfunction      更新时间:2023-10-16

我是 mex 类型编程的新手,基本上我正在编写我的第一个 mexfunction,我遇到了一个愚蠢的问题。我附加了一部分代码:

void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
        int mrows,ncols;
        mrows = mxGetM(prhs[0]);
        ncols = mxGetN(prhs[0]);
        int numElements = ncols;
        size_t size = numElements * sizeof(float);
        float *in_A = (float *)mxMalloc(size);
        float *in_B = (float *)malloc(size);
        float *out_C = (float *)malloc(size);
        if (in_A == NULL || in_B == NULL ||out_C == NULL)
        {
            mexErrMsgTxt("Failed to allocate host vectors!n");
        }
        if (nrhs != 2) 
            mexErrMsgTxt("Two inputs required. A vector and a multiplier");
        if (nlhs != 1) 
            mexErrMsgTxt("One output required. The resulting vector");

        in_A = (float *)mxGetPr(prhs[0]);
            in_B = (float *)mxGetPr(prhs[1]);
        //printf("%f %fn", in_A[2], in_A[1]);
        //mexPrintf("%dn", prhs[1]);
           plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
           out_C = (float *)mxGetPr(plhs[0]);
            add_two_vectors(in_A,in_B,out_C);
}

函数add_two_vectors(float* A,float* B,float* C)正在运行,但是当我在 mexfunction 中运行脚本时,我遇到了问题。此外,它还可以正确读取 nrowsncols 变量。问题是我无法在函数中插入正确的输入。我尝试打印一些矢量元素,但它打印的元素与输入矢量元素不同。我像那样a = addVector([1.1 2.2 355],[2.5 45 5.5])调用 mex函数.有人可以开导我吗?

提前谢谢你。

PS: Matlab 2011a, VS 2010, Win 7 64x.

让我举个例子,我将如何实现这样的函数:

add_vectors.cpp

#include "mex.h"
void add_two_vectors(float *c, const float *a, const float *b, const size_t sz)
{
    for (size_t i=0; i<sz; i++) {
        c[i] = a[i] + b[i];
    }
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // input validation
    if (nrhs != 2 || nlhs > 1) {
        mexErrMsgTxt("Wrong number of input/output arguments.");
    }
    if (!mxIsSingle(prhs[0]) || !mxIsSingle(prhs[1])) {
        mexErrMsgTxt("Inputs must be single arrays.");
    }
    if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1])) {
        mexErrMsgTxt("Inputs must be real arrays.");
    }
    if (mxIsSparse(prhs[0]) || mxIsSparse(prhs[1])) {
        mexErrMsgTxt("Inputs must be dense arrays.");
    }
    if (mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[1])) {
        mexErrMsgTxt("Inputs must have the same size.");
    }
    // create ouput array
    mwSize numel = mxGetNumberOfElements(prhs[0]);
    mwSize ndims = mxGetNumberOfDimensions(prhs[0]);
    const mwSize *dims = mxGetDimensions(prhs[0]);
    plhs[0] = mxCreateNumericArray(ndims, dims, mxSINGLE_CLASS, mxREAL);
    // get pointers to data
    float *c = (float*) mxGetData(plhs[0]);
    float *a = (float*) mxGetData(prhs[0]);
    float *b = (float*) mxGetData(prhs[1]);
    // perform addition: c = a + b
    add_two_vectors(c, a, b, numel);
}

该函数需要两个大小相同的 single 类型的数组(但可以是任何维度,包括标量、向量、矩阵、N-D 数组)。

>> mex -largeArrayDims add_vectors.cpp
>> add_vectors(rand(4,'single'),rand(4,'single'))
ans =
    0.6204    1.2307    1.1657    0.7583
    1.3147    0.3817    0.3059    1.0377
    0.8142    0.3519    0.8619    1.1154
    1.0403    1.1578    0.1365    1.0048

您不能只将mxGetPrdouble*)的输出静态转换为float*而不会误解数据。 将内容读入双精度并转换每个元素,而不是指针。

或者,您可以在 MATLAB 中输入一个带有single(...)的浮点数组,并使用 (float *)mxGetData 而不是 mxGetPr