c++ - 为什么 arraysize 在我的 mex 代码中被识别为零?

标签 c++ matlab visual-c++ visual-studio-2013 mex

我有一个 mex 函数,我在 matlab 中通过以下命令(界面)使用它:

矩阵大小 = 30555

Fv_calc(:,2) = mx_solve_quadratic(QuadraticCoefficients,MatSize);  

网关函数如下:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int *arraysizePtr = NULL;
    arraysizePtr = (int *)mxGetPr(prhs[1]);
    int arraysize = *arraysizePtr;
    float *inMatrix = NULL;
    inMatrix = (float *)mxGetPr(prhs[0]);
    const float a = 1; /* coefficient for x^2 is always 1*/
    plhs[0] = mxCreateNumericMatrix(arraysize, 1, mxSINGLE_CLASS, mxREAL);
    float *out = (float *)mxGetPr(plhs[0]);
    float x0; /* the smaller root */
    float x1; /* the bigger root */
    int fOutput = 0;
    int i = 0;
    for (i = 0; i < arraysize; i++)
    {
        fOutput = gsl_poly_solve_quadratic(a, inMatrix[i], inMatrix[i + arraysize], &x0, &x1);
        out[i] = (x1 > 0 ? x1 : 0);
    }
}  

一切都是真的,因为我之前已经运行过代码,现在我只是做了一点小改动。 实在不明白为什么运行mex代码时arraysize被识别为0

最佳答案

mxGetPr 似乎返回了一个 double *,这是我在网上找到的。

( https://nl.mathworks.com/help/matlab/apiref/mxgetpr.html?s_tid=gn_loc_drop )

转换并将其分配给 int *arraysizePtr 将导致 double 数据在通过 *arraysizePtr 访问时被解释为 int ,产生废话。

关于c++ - 为什么 arraysize 在我的 mex 代码中被识别为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419295/

相关文章:

c++ - 更改 cout 文本的颜色 C++

matlab - 在编辑器中自定义数据提示

matlab - MATLAB GUIDE 中的按钮回调类型

c++ - char* 加倍并再次返回 char*(64 位应用程序)

C++ FIFO 奇怪的行为

c++ - C++ VS2010 中的 "for each"循环编译错误

c++ - NULL 指针并删除它

image - Matlab:在不同坐标上的单轴上绘制多个图像

C++ 使用裸函数

visual-studio - 跳过函数输入参数的复制构造函数的调试?