Return variable number of exits from mex function

Is there a way to return a variable number of exits from a mex function?

You can pack them into a cell, but I wondered if there is a way for them to be expanded directly in the output list. Sort of

a = mymex(arg1, arg2);

[a, b, c] = mymex(arg1, arg2, 'all');
+4
source share
2 answers

Of course, you can, like any other MATLAB function:

test_mex.cpp

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    for (int i=0; i<nlhs; i++) {
        plhs[i] = mxCreateDoubleScalar(i);
    }
}

MATLAB

>> [a,b,c] = test_mex()
a =
     0
b =
     1
c =
     2

, /, , ( , ..). ( )

, , :

>> out = cell(1,20);
>> [out{:}] = test_mex()
out = 
  Columns 1 through 11
    [0]    [1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]    [9]    [10]
  Columns 12 through 20
    [11]    [12]    [13]    [14]    [15]    [16]    [17]    [18]    [19]

20 :

>> [x1,x2,...,x20] = test_mex()

EDIT:

, MEX- M-, ( function varargout = mymex(varargin)), ; .

, M-, , :

function varargout = test_fcn(varargin)
    for i=1:nargout
        varargout{i} = i-1;
    end
end

, MEX MATLAB, - , , .

:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    for (int i=0; i<30; i++) {    // <--- note 30 as value
        plhs[i] = mxCreateDoubleScalar(i);
    }
}

:

>> test_mex    % careful!

MATLAB. M-, , .

@chappjc, MEX , , (plhs - mxArray* 1 , plhs[0] , ). LHS, , ans (, ).

, , MATLAB catch MATLAB:unassignedOutputs ( MEX, M-).

, (MATLAB , ", MATLAB" ). M-, "Index exceeds matrix dimensions.", !

, MEX- ( ) , /.

+4

MEX MATLAB. , MEX, / mexFunction ( nlhs nrhs, ).

mexFunction mex.h ( 141 R2014b):

/*
 * mexFunction is the user-defined C routine that is called upon invocation
 * of a MEX-function.
 */
void mexFunction(
    int           nlhs,           /* number of expected outputs */
    mxArray       *plhs[],        /* array of pointers to output arguments */
    int           nrhs,           /* number of inputs */
    const mxArray *prhs[]         /* array of pointers to input arguments */
);

main C/++, . (int main(int argc, const char* argv[])), count pointer (argv[0] ), mexFunction , .

nlhs nrhs . . l eft h s, r ight h s.


, - mexFunction ans MATLAB, , (), . MEX nlhs. nlhs=0, plhs[0] ans, . MEX . , :

// test_nlhs.cpp
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    for (int i=0; i<nlhs; ++i)
        plhs[i] = mxCreateDoubleScalar(i);
}

:

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

ans, nlhs plhs[0]. :

// test_nlhs.cpp
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if (nlhs==0) {
        mexPrintf("Returning special value to ""ans"".\n");
        plhs[0] = mxCreateDoubleScalar(-1);
    } else {
        mexPrintf("Returning to specified workspace variables.\n");
        for (int i=0; i<nlhs; ++i)
            plhs[i] = mxCreateDoubleScalar(i);
    }
}

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

, , , , , .

+2

All Articles