Écrire des fonctions C appelables depuis MATLAB (fichiers MEX)
mxArray pour accéder aux variables MATLAB et créer des tableaux à renvoyer à MATLABVous pouvez appeler vos propres programmes C à partir de la ligne de commande MATLAB comme s’il s’agissait de fonctions prédéfinies. Ces programmes sont appelés fonctions MEX. Le nom de la fonction est le nom du fichier MEX. Les fonctions MEX ne conviennent pas à toutes les applications. MATLAB est un environnement à haute productivité spécialement conçu pour éliminer les tâches chronophages de programmation de bas niveau dans les langages compilés. De manière générale, la programmation doit s’effectuer dans MATLAB. Utilisez des fonctions MEX seulement si elles sont indispensables à votre application.
Pour créer une fonction MEX, écrivez votre programme avec les API MATLAB. Les fonctions de ces bibliothèques facilitent le transfert de données entre les fonctions MEX et l’espace de travail. Pour choisir une API MATLAB, tenez compte des points suivants :
Créez des fonctions MEX avec les fonctionnalités du C++ moderne définies dans MATLAB Data API pour C++. Pour plus d’informations, consultez Écrire des fonctions C++ appelables depuis MATLAB (fichiers MEX).
Si vos fonctions MEX doivent s’exécuter dans MATLAB R2017b ou version antérieure ou si vous préférez le langage C, utilisez les fonctions de C Matrix API et de MEX C API. Ces API utilisent la structure de données MATLAB
mxArray. Les fonctions et rubriques de cette page sont basées surmxArray.
Remarque
Choisissez soit les fonctions de C Matrix API, soit celles de MATLAB Data Array API. Vous ne pouvez pas combiner les fonctions de ces API.
Compilez votre fichier source en programme exécutable avec la fonction mex. Vous pouvez également partager le fichier MEX avec d’autres utilisateurs MATLAB.
Pour plus d’informations sur l’écriture de S-functions, consultez votre documentation Simulink®.
Pour appeler une fonction MEX créée par un autre utilisateur, consultez Appeler des fonctions MEX.
Si vous souhaitez appeler des fonctions MATLAB depuis un programme C/C++, utilisez l’une des API Engine. Pour plus d’informations, consultez :
Si vous souhaitez lire et écrire des données MATLAB depuis des programmes C, consultez Écrire des programmes C pour lire les données de fichiers MAT.
Fonctions
MEX C API
mexFunction | Entry point to C/C++ MEX function built with C Matrix API |
mexFunctionName | Name of current MEX function |
mexAtExit | Register function to call when MEX function clears or MATLAB terminates |
mexCallMATLAB | Call MATLAB function, user-defined function, or MEX function |
mexCallMATLABWithTrap | Call MATLAB function, user-defined function, or MEX file and capture error information |
mexEvalString | Execute MATLAB command in caller workspace |
mexEvalStringWithTrap | Execute MATLAB command in caller workspace and capture error information |
mexGetVariable | Copy of variable from specified workspace |
mexGetVariablePtr | Read-only pointer to variable from another workspace |
mexPutVariable | Array from MEX function into specified workspace |
mexGet | Value of specified graphics property |
mexSet | Set value of specified graphics property |
mexPrintf | ANSI C PRINTF-style output routine |
mexErrMsgIdAndTxt | Display error message with identifier and return to MATLAB prompt |
mexWarnMsgIdAndTxt | Warning message with identifier |
mexIsLocked | Determine if MEX file is locked |
mexLock | Prevent clearing MEX file from memory |
mexUnlock | Allow clearing MEX file from memory |
mexMakeArrayPersistent | Make array persist after MEX file completes |
mexMakeMemoryPersistent | Make memory allocated by MATLAB persist after MEX function completes |
Rubriques
Écrire des fonctions MEX C
- Create C Source MEX File arrayProduct.c
This example shows how to create the
arrayProductC MEX function built with the C Matrix API. - Tables of MEX Function Source Code Examples
Links to source code for MEX function examples.
- MATLAB Data
Using
mxArrayin MEX files. - Memory Management Issues
Rules for managing
mxArraymemory. - Create C++ MEX Functions with C Matrix API
C++ language issues to consider when creating MEX functions built with the C Matrix API. - Error Handling in C MEX Files
Print error information using themexErrMsgIdAndTxtfunction. - Handling Large File I/O in MEX Files
How to use 64-bit file I/O in your MEX file. - Testing for Most-Derived Class
How to exclude subclasses of built-in types from MEX file input arguments.
Tester votre environnement de compilation
- Build C MEX Function
This example shows how to build the example C MEX function
arrayProduct.
Versions de C Matrix API
- Upgrade MEX Files to Use Interleaved Complex API
How to use an interleaved representation of complex numbers in MEX files.
- Upgrade MEX Files to Use 64-Bit API
How to build MEX files with the 64-bit API by default.
- Upgrade MEX Files to Use Graphics Objects
How to change the data type of handles to graphics objects fromdoubletoobject.
Partager des fichiers MEX
- Document Build Information in the MEX File
This example shows how to document thextimesyMEX file built on a Windows® platform using a Microsoft® Visual C++® compiler.
Résolution des problèmes
Erreurs de compilation
- Getting Help When MEX Fails
To help diagnose compiler set up and build errors, call themexfunction with verbose option -v. - Compiling MEX File Fails
What to do when compiling a MEX file fails. - Troubleshooting MEX API Incompatibilities
More information for warnings and error messages. - Troubleshooting and Limitations Compiling C/C++ MEX Files with MinGW-w64
Troubleshooting MEX files built with the MinGW-w64 compiler. - Symbol mexFunction Unresolved or Not Defined
Every MEX file needs amexFunction.
Erreurs run-time
- Invalid MEX File Errors
What to do when you get an invalid MEX file error. - MEX Version Compatibility
For best results, run MEX files built with your MATLAB version. - MEX Platform Compatibility
If you get a binary MEX file from another source, be sure that the file was compiled for the same platform on which you want to run it. - MEX API Is Not Thread Safe
Do not call a single session of MATLAB on separate threads from a MEX file. - MEX File Segmentation Fault
What to do when a MEX file causes a segmentation violation or assertion. - MEX File Generates Incorrect Results
What to do when your MEX generates wrong answers.
Débuggage
- Debug on Microsoft Windows Platforms
Suggestions for debugging with Microsoft Visual Studio®. - Debug on macOS Platforms
This example shows how to debug a MEX file on Mac Platforms. - Debug on Linux Platforms
These examples show how to debug a MEX file on Linux® platforms.