Main Content

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

CallmexCallMATLABto invoke internal MATLAB®numeric functions, MATLAB operators, user-defined functions, or other MEX functions.

BothmexCallMATLABandmexEvalStringexecute MATLAB commands.UsemexCallMATLABfor returning results (left side arguments) back to the MEX function. ThemexEvalStringfunction does not return values to the MEX function.

Input Arguments

expand all

Number of expected outputmxArrays, specified as an integer less than or equal to 50.

Array of pointers to themxArrayoutput arguments.

Caution

Theplhsargument formexCallMATLABis not the same as theplhsformexFunction。Do not destroy anmxArrayreturned inplhsformexFunction

Number of inputmxArrays, specified as an integer less than or equal to 50.

Array of pointers to themxArrayinput arguments.

Name of the MATLAB built-in function, operator, user-defined function, or MEX function to call specified asconst char*

IffunctionNameis an operator, place the operator inside a pair of double quotes, for example,"+"

Output Arguments

expand all

The function returns0if successful, and a nonzero value if unsuccessful.

Error Handling

IffunctionNamedetects an error, MATLAB terminates the MEX function and returns control to the MATLAB prompt. To trap errors, use themexCallMATLABWithTrapfunction.

Limitations

  • Avoid using themexCallMATLABfunction in Simulink®S-functions. If you do, do not store the resultingplhsmxArraypointers in any S-function block state that persists after the MEX function finishes. Outputs ofmexCallMATLABhave temporary scope and are automatically destroyed at the end of the MEX function call.

  • It is possible to generate an object of typemxUNKNOWN_CLASSusingmexCallMATLAB。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 callfoousingmexCallMATLAB, the unassigned output variable is now typemxUNKNOWN_CLASS

Examples

See these examples inmatlabroot/extern/examples/mex:

See these examples inmatlabroot/extern/examples/refbook:

See these examples inmatlabroot/extern/examples/mx:

Tips

  • MATLAB allocates dynamic memory to store the arrays inplhsformexCallMATLAB。MATLAB自动化lly deallocates the dynamic memory when you exit the MEX function. However, if heap space is at a premium, callmxDestroyArraywhen you are finished with the arrays inplhs

Introduced before R2006a