Verfassen von C Funktionen, die aus MATLAB heraus aufgerufen werden können (MEX-Dateien)
mxArray
zum Zugriff auf MATLAB-Variablen und zur Erstellung von Arrays, die an MATLAB zurückgegeben werdenSie können Ihre eigenen C Programme über die MATLAB-Befehlszeile aufrufen, als wären es eingebaute Funktionen. Diese Programme werden als MEX-Funktionen bezeichnet. Der Name der Funktion entspricht dem MEX-Dateinamen. MEX-Funktionen eignen sich nicht für alle Anwendungsbereiche. MATLAB ist eine Hochproduktivitätsumgebung, die insbesondere zeitraubende Low-Level-Programmierarbeiten in kompilierten Sprachen überflüssig macht. Im Allgemeinen findet die Programmierung in MATLAB statt. Verwenden Sie MEX-Funktionen nur dann, wenn die Anwendung dies erfordert.
Verfassen Sie zur Erstellung einer MEX-Funktion Ihre eigenen Programme mithilfe von MATLAB-APIs. Die Funktionen in diesen beiden Bibliotheken vereinfachen die Weitergabe von Daten zwischen MEX-Funktionen und dem Workspace. Beachten Sie Folgendes bei der Auswahl einer MATLAB-API:
Erstellen Sie MEX-Funktionen mithilfe moderner C++ Funktionen, wie unter MATLAB-Daten-API für C++ definiert. Weitere Informationen finden Sie unter Verfassen von C++ Funktionen, die von MATLAB aus aufgerufen werden können (MEX-Dateien).
Wenn Ihre MEX-Funktionen in MATLAB R2017b oder früher ausgeführt werden müssen oder wenn Sie lieber mit der Sprache C arbeiten, dann verwenden Sie Funktionen in der C Matrix-API und der C MEX-API. Diese APIs nutzen die Datenstruktur der MATLAB-Funktion
mxArray
. Die Funktionen und Artikel auf dieser Seite basieren aufmxArray
.
Hinweis
Wählen Sie Funktionen der C Matrix-API oder der MATLAB-Daten-API. Sie können die Funktionen aus diesen APIs nicht miteinander kombinieren.
Erstellen Sie Ihre Quelldatei mithilfe der Funktion mex
in einem ausführbaren Programm. Sie können auch die MEX-Datei für andere MATLAB-Benutzer freigeben.
Informationen zum Verfassen von S-Funktionen finden Sie in Ihrer Dokumentation zu Simulink®.
Informationen zum Aufruf einer von einer anderen Person erstellten MEX-Funktion finden Sie unter Aufrufen von MEX-Funktionen.
Wenn Sie in einem C/C++ Programm MATLAB Funktionen aus dem Programm heraus aufrufen möchten, dann verwenden Sie eine der Engine-APIs. Weitere Informationen finden Sie hier:
Informationen dazu, wie Sie MATLAB-Daten aus C Programmen lesen und schreiben können, finden Sie unter Verfassen von C Programmen zum Lesen von MAT-File-Daten.
Funktionen
C MEX-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 |
Themen
Verfassen von C MEX-Funktionen
- Create C Source MEX File arrayProduct.c
This example shows how to create the
arrayProduct
C 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
mxArray
in MEX files. - Memory Management Issues
Rules for managing
mxArray
memory. - 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 themexErrMsgIdAndTxt
function. - 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.
Testen der Build-Umgebung
- Build C MEX Function
This example shows how to build the example C MEX function
arrayProduct
.
C Matrix-API-Versionen
- Upgrade MEX Files to Use Interleaved Complex API
MATLAB Version 9.4 (R2018a) supports an interleaved representation of complex numbers.
- Upgrade MEX Files to Use 64-Bit API
MATLAB Version 9.2 (R2017a) builds MEX files with the 64-bit API by default.
- Upgrade MEX Files to Use Graphics Objects
MATLAB Version 8.4 (R2014b) changes the data type of handles to graphics objects fromdouble
to object.
Freigeben von MEX-Dateien
- Document Build Information in the MEX File
This example shows how to document thextimesy
MEX file built on a Windows® platform using a Microsoft® Visual C++® compiler.
Fehlersuche
Build-Fehler
- Getting Help When MEX Fails
To help diagnose compiler set up and build errors, call themex
function 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
.
Laufzeitfehler
- 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.
Debugging
- 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
This example shows how to debug a MEX file on Linux® Platforms.