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++) {
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- ( ) , /.