Verfassen von C++ Funktionen, die von MATLAB aus aufgerufen werden können (MEX-Dateien)
Verwenden Sie zur Erstellung von MATLAB-Funktionen, die die Effizienz von C++ Programmen haben und von MATLAB aus auf externe Funktionen und Bibliotheken zugreifen können, die C++ MEX-API. Die C++ MEX-API bietet die folgenden Funktionen:
Es werden moderne C++ Programmierungsfunktionen und objektorientierte Designs verwendet.
MATLAB-Funktionen werden aus der MEX-Funktion heraus aufgerufen.
Nahtlose Integration in MATLAB, d. h. Abrufen von Eingaben aus und Senden von Ergebnissen an MATLAB.
Es werden MATLAB-Datentypen und Copy-On-Write-Verfahren unterstützt.
Es werden Thread-sichere Multi-Thread-MEX-Funktionen verfasst.
C++ MEX-Funktionen werden prozessextern (Out-of-Process) ausgeführt.
Hinweis
Funktionen in den MATLAB C++ APIs nutzen die Klasse matlab::data::Array
, die in MATLAB-Daten-API für C++ definiert ist. Wenn Ihre MEX-Funktionen in MATLAB R2017b oder früher ausgeführt werden müssen oder wenn Sie lieber mit der Sprache C arbeiten, verwenden Sie C Matrix-API, da dort die mxArray
-Datenstruktur zum Einsatz kommt. Weitere Informationen finden Sie unter Verfassen von C Funktionen, die aus MATLAB heraus aufgerufen werden können (MEX-Dateien).
Themen
Erstellen von C++ MEX-Funktionen
- C++ MEX Functions
MEX files implement MATLAB functions written in C++. - Create a C++ MEX Source File
How to create a basic C++ MEX function.
Einrichtung und Erstellung von MEX-Dateien
- Build C++ MEX Programs
Build MEX applications on different platforms. - Test Your Build Environment
Test your build environment with a predefined C++ MEX file. - C++ MEX API
Use C++11 features and the C++ Engine API to implement MEX functions.
Design von C++ MEX-Funktionen
- Structure of C++ MEX Function
Implement a class that overrides the function call operator()
to create a functor object. - Avoid Copies of Arrays in MEX Functions
Techniques to avoid making copies of large arrays to reduce memory usage and improve execution speed. - Managing External Resources from MEX Functions
Customize theMexFunction
class to better handle the resources used by your MEX function. - Handling Inputs and Outputs
Check the size and type of inputs and display error messages in MATLAB. - Displaying Output in MATLAB Command Window
MEX functions can display output in the MATLAB command window. - Making async Requests Using mexCallMATLAB
Behavior when calling back into MATLAB with anasync
request likematlab::engine::MATLABEngine::fevalAsync
.
MATLAB Datentypen in MEX-Funktionen
- Data Access in Typed, Cell, and Structure Arrays
Cell and structure arrays let you write to array contents without copying entire array. - Data Types for Passing MEX Function Data
Define the correct data types to pass data between MATLAB and MEX functions.
Aufrufen von MATLAB-Funktionen
- Call MATLAB Functions from MEX Functions
Call MATLAB functions, passing arguments from and returning arguments to a MEX function. - Catch Exceptions in MEX Function
Catch exceptions thrown in a MEX function. - Execute MATLAB Statements from MEX Function
Execute MATLAB statements to create variables in the caller's workspace.
Separate Threads und Prozesse
- Call MATLAB from Separate Threads in MEX Function
MEX functions can call MATLAB from user-created threads using the C++ Engine asynchronous API. - Out-of-Process Execution of C++ MEX Functions
Execute C++ MEX functions in processes that are separate from the MATLAB process.
Zugriff auf MATLAB-Variablen
- Set and Get MATLAB Variables from MEX
Put variables into or get variables from the MATLAB base and global workspaces. - MATLAB Objects in MEX Functions
Pass MATLAB objects to MEX functions. - Using MEX Functions for MATLAB Class Methods
You can use MEX functions to implement methods for MATLAB classes.