Main Content

Write C Functions Callable from MATLAB (MEX Files)

Create C functions that can be called from MATLAB® R2017b or earlier, using mxArray to access MATLAB variables and create arrays to return to MATLAB

You can call your own C programs from the MATLAB command line as if they were built-in functions. These programs are called MEX functions, and the function name is the MEX file name. MEX functions are not appropriate for all applications. MATLAB is a high-productivity environment whose specialty is eliminating time-consuming, low-level programming in compiled languages. In general, do your programming in MATLAB. Do not use MEX functions unless your application requires it.

To create a MEX function, write your programs using MATLAB APIs. The functions in these libraries facilitate the transfer of data between MEX functions and the workspace. To choose a MATLAB API, consider the following:

Note

Choose functions from either the C Matrix API or the MATLAB Data Array API; you cannot mix functions from these APIs.

Build your source file into an executable program using the mex function. You can also share the MEX file with other MATLAB users.

For information about writing S-functions, see your Simulink® documentation.

To call a MEX function that someone else created, see Call MEX Functions.

If you have a C/C++ program and want to call MATLAB functions from the program, then use one of the engine APIs. For more information, see:

If you want to read and write MATLAB data from C programs, see Write C Programs to Read MAT-File Data.

Functions

mexBuild MEX function or engine application
dbmexEnable MEX-file debugging on UNIX platforms

C MEX API

mexFunctionEntry point to C/C++ MEX function built with C Matrix API
mexFunctionNameName of current MEX function
mexAtExitRegister function to call when MEX function clears or MATLAB terminates
mexCallMATLABCall MATLAB function, user-defined function, or MEX function
mexCallMATLABWithTrapCall MATLAB function, user-defined function, or MEX file and capture error information
mexEvalStringExecute MATLAB command in caller workspace
mexEvalStringWithTrapExecute MATLAB command in caller workspace and capture error information
mexGetVariableCopy of variable from specified workspace
mexGetVariablePtrRead-only pointer to variable from another workspace
mexPutVariableArray from MEX function into specified workspace
mexGetValue of specified graphics property
mexSetSet value of specified graphics property
mexPrintfANSI C PRINTF-style output routine
mexErrMsgIdAndTxtDisplay error message with identifier and return to MATLAB prompt
mexWarnMsgIdAndTxtWarning message with identifier
mexIsLockedDetermine if MEX file is locked
mexLockPrevent clearing MEX file from memory
mexUnlockAllow clearing MEX file from memory
mexMakeArrayPersistentMake array persist after MEX file completes
mexMakeMemoryPersistentMake memory allocated by MATLAB persist after MEX function completes

Topics

Write C MEX Functions

C Matrix API Versions

Share MEX Files

Troubleshooting

Build Errors

Runtime Errors

Debugging