matlab - Return variable number of outputs from mex function -



matlab - Return variable number of outputs from mex function -

is there way homecoming variable number of outputs mex function?

one might pack them cell, wondered wether there way, expanded straight in output list. like

a = mymex(arg1, arg2); [a, b, c] = mymex(arg1, arg2, 'all');

the syntax calling mex function identical other matlab function. however, internal mex function, number of in/out arguments used determined first , 3rd arguments mexfunction (usually named nlhs , nrhs, can anything).

declaration of mexfunction in mex.h (around line 141 in r2014b):

/* * mexfunction user-defined c routine called upon invocation * of mex-function. */ void mexfunction( int nlhs, /* number of expected outputs */ mxarray *plhs[], /* array of pointers output arguments */ int nrhs, /* number of inputs */ const mxarray *prhs[] /* array of pointers input arguments */ );

this not unlike syntax standard main functions c/c++ command line executables, there notable differences. unlike native command line version (int main(int argc, const char* argv[])), count , pointer array not include name of function (argv[0] name of executable programme file), , mexfunction there parameters output arguments input.

the naming nlhs , nrhs should clear. hint: in mathematics, equation has left hand side , right hand side.

an of import overlooked quality of mexfunction i/o handling pertains matlab's special ans variable, place function outputs (often) go if don't assign variable. in mex file, need remember next when checking nlhs. if nlhs=0, can , must still write plhs[0] ans used. it's documented, barely, in data flow in mex-files. consider happens next code:

// test_nlhs.cpp void mexfunction(int nlhs, mxarray *plhs[], int nrhs, const mxarray *prhs[]) { (int i=0; i<nlhs; ++i) plhs[i] = mxcreatedoublescalar(i); }

outputs:

>> test_nlhs >> [a,b] = test_nlhs = 0 b = 1

there's no output ans because logic nlhs prevents assigning plhs[0]. code:

// test_nlhs.cpp void mexfunction(int nlhs, mxarray *plhs[], int nrhs, const mxarray *prhs[]) { if (nlhs==0) { mexprintf("returning special value ""ans"".\n"); plhs[0] = mxcreatedoublescalar(-1); } else { mexprintf("returning specified workspace variables.\n"); (int i=0; i<nlhs; ++i) plhs[i] = mxcreatedoublescalar(i); } }

outputs

>> test_nlhs returning special value ans. ans = -1 >> [a,b] = test_nlhs returning specified workspace variables. = 0 b = 1

it's doesn't have special value, , it should same first output argument, i'm illustrating how recognize calling syntax.

matlab return-value mex

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -