Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何形成C++;具有Matlab矩阵输入的2D阵列? 我试图用Matlab MEX函数构造一些C++源代码。我有一个Matlab格式的输入(矩阵格式传递到*prhs[]如下)。当将其传递到MEXTULL时,我想将其存储为C++ 2D数组格式。我写了一些如下的代码,但无法编译。你能给我一些建议吗?非常感谢。A void mexFunction( int nlhs, mxArray *[], int nrhs, const mxArray *prhs[]) { unsigned int** raster; col_rast = mxGetN(prhs[0]); row_rast = mxGetM(prhs[0]); raster = mxCalloc(col_rast , row_rast ); // Sorry, I missed this line when copy and paste. // Convert the matrix into a 2D C array for (int col=0; col < col_rast; col++) { for (int row=0; row < row_rast; row++) { raster[col][row] = mxGetPr(prhs[0])[row+col*row_rast]; } } void mexFunction( 国际nlhs, mxArray*[], 国际nrhs, 常量mxArray*prhs[] { 无符号整数**光栅; col_rast=mxGetN(prhs[0]); row_rast=mxGetM(prhs[0]); raster=mxCalloc(col_rast,row_rast);//对不起,复制和粘贴时我错过了这一行。 //将矩阵转换为2D C数组 for(int col=0;col_C++_Arrays_Matlab_Matrix_Mex - Fatal编程技术网

如何形成C++;具有Matlab矩阵输入的2D阵列? 我试图用Matlab MEX函数构造一些C++源代码。我有一个Matlab格式的输入(矩阵格式传递到*prhs[]如下)。当将其传递到MEXTULL时,我想将其存储为C++ 2D数组格式。我写了一些如下的代码,但无法编译。你能给我一些建议吗?非常感谢。A void mexFunction( int nlhs, mxArray *[], int nrhs, const mxArray *prhs[]) { unsigned int** raster; col_rast = mxGetN(prhs[0]); row_rast = mxGetM(prhs[0]); raster = mxCalloc(col_rast , row_rast ); // Sorry, I missed this line when copy and paste. // Convert the matrix into a 2D C array for (int col=0; col < col_rast; col++) { for (int row=0; row < row_rast; row++) { raster[col][row] = mxGetPr(prhs[0])[row+col*row_rast]; } } void mexFunction( 国际nlhs, mxArray*[], 国际nrhs, 常量mxArray*prhs[] { 无符号整数**光栅; col_rast=mxGetN(prhs[0]); row_rast=mxGetM(prhs[0]); raster=mxCalloc(col_rast,row_rast);//对不起,复制和粘贴时我错过了这一行。 //将矩阵转换为2D C数组 for(int col=0;col

如何形成C++;具有Matlab矩阵输入的2D阵列? 我试图用Matlab MEX函数构造一些C++源代码。我有一个Matlab格式的输入(矩阵格式传递到*prhs[]如下)。当将其传递到MEXTULL时,我想将其存储为C++ 2D数组格式。我写了一些如下的代码,但无法编译。你能给我一些建议吗?非常感谢。A void mexFunction( int nlhs, mxArray *[], int nrhs, const mxArray *prhs[]) { unsigned int** raster; col_rast = mxGetN(prhs[0]); row_rast = mxGetM(prhs[0]); raster = mxCalloc(col_rast , row_rast ); // Sorry, I missed this line when copy and paste. // Convert the matrix into a 2D C array for (int col=0; col < col_rast; col++) { for (int row=0; row < row_rast; row++) { raster[col][row] = mxGetPr(prhs[0])[row+col*row_rast]; } } void mexFunction( 国际nlhs, mxArray*[], 国际nrhs, 常量mxArray*prhs[] { 无符号整数**光栅; col_rast=mxGetN(prhs[0]); row_rast=mxGetM(prhs[0]); raster=mxCalloc(col_rast,row_rast);//对不起,复制和粘贴时我错过了这一行。 //将矩阵转换为2D C数组 for(int col=0;col,c++,arrays,matlab,matrix,mex,C++,Arrays,Matlab,Matrix,Mex,编辑1: 错误是: 错误C2440:“=”:无法从“void*”转换为“unsigned short” 从“void”转换为指向非“void”的指针需要显式强制转换 编辑2: 但Matlab内置示例(arrayFillSetData.c)的类似实现没有问题: #include "mex.h" /* The mxArray in this example is 2x2 */ #define ROWS 2 #define COLUMNS 2 #define ELEMENTS 4 void mex

编辑1:

错误是:

错误C2440:“=”:无法从“void*”转换为“unsigned short” 从“void”转换为指向非“void”的指针需要显式强制转换

编辑2:

但Matlab内置示例(arrayFillSetData.c)的类似实现没有问题:

#include "mex.h"

/* The mxArray in this example is 2x2 */
#define ROWS 2
#define COLUMNS 2
#define ELEMENTS 4

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    UINT16_T *dynamicData;                        /* pointer to dynamic data */
    const UINT16_T data[] = {2, 3, 2, 1};  /* existing data */
    mwSize index;

    /* Check for proper number of arguments. */
    if ( nrhs != 0 ) {
        mexErrMsgIdAndTxt("MATLAB:arrayFillSetData:rhs",
                "This function takes no input arguments.");
    } 

    /* Create a local array and load data */
    dynamicData = mxCalloc(ELEMENTS, sizeof(UINT16_T));
    for ( index = 0; index < ELEMENTS; index++ ) {
        dynamicData[index] = data[index];
    }

    /* Create a 0-by-0 mxArray; you will allocate the memory dynamically */
    plhs[0] = mxCreateNumericMatrix(0, 0, mxUINT16_CLASS, mxREAL);

    /* Point mxArray to dynamicData */
    mxSetData(plhs[0], dynamicData);
    mxSetM(plhs[0], ROWS);
    mxSetN(plhs[0], COLUMNS);

    /* Do not call mxFree(dynamicData) because plhs[0] points to dynamicData */

    return;
}
#包括“mex.h”
/*本例中的mxArray是2x2*/
#定义第2行
#定义第2列
#定义要素4
void MEX函数(int nlhs、mxArray*plhs[]、int nrhs、const mxArray*prhs[]){
UINT16\u T*动态CDATA;/*指向动态数据的指针*/
const UINT16_T data[]={2,3,2,1};/*现有数据*/
分子量大小指数;
/*检查参数的数量是否正确*/
如果(nrhs!=0){
MEXERMSGIDANDTXT(“MATLAB:arrayFillSetData:rhs”,
“此函数不接受输入参数。”);
} 
/*创建本地数组并加载数据*/
dynamicData=mxCalloc(元素,大小(UINT16_T));
对于(索引=0;索引<元素;索引++){
动态CDATA[index]=数据[index];
}
/*创建一个0×0的mxArray;您将动态分配内存*/
plhs[0]=mxCreateNumericMatrix(0,0,mxUINT16_类,mxREAL);
/*指向动态CDATA的点mxArray*/
mxSetData(plhs[0],动态CDATA);
mxSetM(plhs[0],行);
mxSetN(plhs[0],列);
/*不要调用mxFree(dynamicATA),因为plhs[0]指向dynamicATA*/
返回;
}
编辑3:


我想我找到了问题所在。我将Matlab内置示例从'arrayFillSetData.c'重命名为'arrayFillSetData.cpp',然后出现错误

arrayFillSetData.cpp(35):错误C2440:“=”:无法从“void*”转换为“unsigned short”
从“void”到指向非“void”的指针的转换需要显式转换

编译器错误是什么?您可能希望为
光栅
分配缓冲区…抱歉,我在最初发布此文章时错过了分配缓冲区的一行。请查看我的编辑,仍然会出现错误。非常感谢。我想我发现了问题。我将将Matlab内置示例从'arrayFillSetData.c'转换为'arrayFillSetData.cpp',然后出现错误。请参阅上面的我的edit3。如何解决此问题?找到解决方案: