简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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