Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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
如何从Matlab C Mex函数中获得两个输出?_C_Matlab - Fatal编程技术网

如何从Matlab C Mex函数中获得两个输出?

如何从Matlab C Mex函数中获得两个输出?,c,matlab,C,Matlab,我知道如何编写一个基本的C Mex函数,输出类型为double。我试图写一个有两个输出的cmex,但是我得到了分段冲突错误。第一个输出是双精度的,第二个输出是整数。下面是我尝试分配输出指针的代码: plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); //works fine plhs[1] = mxCreateNumericArray(1, 1, mxINT32_CLASS, mxREAL); //causes segmentation violation

我知道如何编写一个基本的C Mex函数,输出类型为double。我试图写一个有两个输出的cmex,但是我得到了分段冲突错误。第一个输出是双精度的,第二个输出是整数。下面是我尝试分配输出指针的代码:

plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); //works fine
plhs[1] = mxCreateNumericArray(1, 1, mxINT32_CLASS, mxREAL); //causes segmentation violation

我在互联网上搜索过,但几乎所有的示例都只有一个或多个相同类型的输出。如何获得两个输出,一个是double类型,另一个是integer类型?

首先,您错误地调用了mxCreateNumericArray。您需要这样做:

#include "mex.h"

void mexFunction( int nlhs, mxArray * plhs[], 
                  int nrhs, const mxArray * prhs[] ) {
    plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
    if ( nlhs > 1 ) {
        mwSize nd = 2;
        mwSize dims[] = { 3, 4 };
        plhs[1] = mxCreateNumericArray(nd, dims, mxINT32_CLASS, mxREAL);
    }
}

首先,您错误地调用了mxCreateNumericArray。您需要这样做:

#include "mex.h"

void mexFunction( int nlhs, mxArray * plhs[], 
                  int nrhs, const mxArray * prhs[] ) {
    plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
    if ( nlhs > 1 ) {
        mwSize nd = 2;
        mwSize dims[] = { 3, 4 };
        plhs[1] = mxCreateNumericArray(nd, dims, mxINT32_CLASS, mxREAL);
    }
}

请发布一个完整的、最少的示例和两个mex函数文档的链接。请发布一个完整的、最少的示例和两个mex函数文档的链接。