简体   繁体   English

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

[英]How to get two outputs from a Matlab C Mex function?

I know how to write a basic C Mex function with one output of type double. 我知道如何用double类型的一个输出编写基本的C Mex函数。 I tried to write a C Mex with two outputs but I got segmentation violation errors. 我尝试编写具有两个输出的C Mex,但是遇到了细分违规错误。 The first output is a double, the second an integer. 第一个输出为双精度,第二个为整数。 Here is the code where I try to assign the output pointers: 这是我尝试分配输出指针的代码:

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

I searched the internet, but almost all the examples have just one output or outputs of the same type. 我搜索了互联网,但是几乎所有示例都只有一个或多个相同类型的输出。 What should be done to get two outputs, one of type double, the other of type integer? 如何获得两个输出,一个输出为double类型,另一个输出为integer类型?

Firstly, you're calling mxCreateNumericArray incorrectly. 首先,您错误地调用了mxCreateNumericArray。 You need to do something like this: 您需要执行以下操作:

#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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM