mexCallMATLAB (C)
CallMATLABfunction, user-defined function, or MEX function
C Syntax
#include "mex.h" int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *functionName);
Description
Note
使用现代c++写墨西哥人功能features and theMATLAB Data API, seeC++ MEX Applications。
CallmexCallMATLAB
to invoke internal MATLAB®numeric functions, MATLAB operators, user-defined functions, or other MEX functions.
BothmexCallMATLAB
andmexEvalString
execute MATLAB commands.UsemexCallMATLAB
for returning results (left side arguments) back to the MEX function. ThemexEvalString
function does not return values to the MEX function.
Input Arguments
Output Arguments
Error Handling
IffunctionName
detects an error, MATLAB terminates the MEX function and returns control to the MATLAB prompt. To trap errors, use themexCallMATLABWithTrap
function.
Limitations
Avoid using the
mexCallMATLAB
function in Simulink®S-functions. If you do, do not store the resultingplhs
mxArray
pointers in any S-function block state that persists after the MEX function finishes. Outputs ofmexCallMATLAB
have temporary scope and are automatically destroyed at the end of the MEX function call.It is possible to generate an object of type
mxUNKNOWN_CLASS
usingmexCallMATLAB
。For example, this function returns two variables but only assigns one of them a value.function [a,b] = foo(c) a = 2*c;
If you then call
foo
usingmexCallMATLAB
, the unassigned output variable is now typemxUNKNOWN_CLASS
。
Examples
See these examples in
:matlabroot
/extern/examples/mex
See these examples in
:matlabroot
/extern/examples/refbook
See these examples in
:matlabroot
/extern/examples/mx
Tips
MATLAB allocates dynamic memory to store the arrays in
plhs
formexCallMATLAB
。MATLAB自动化lly deallocates the dynamic memory when you exit the MEX function. However, if heap space is at a premium, callmxDestroyArray
when you are finished with the arrays inplhs
。