开发者

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

开发者 https://www.devze.com 2023-03-13 21:17 出处:网络
I know how to write a basic C Mex function with one output of type double. I tried to write a C Mex with two outputs but I got segmentation violation errors. The first output is a double, the second a

I know how to write a basic C Mex function with one output of type double. I tried to write a C Mex with two outputs but I got segmentation violation errors. 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?


Firstly, you're calling mxCreateNumericArray incorrectly. 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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号