C with MATLAB
loadlibrary and
calllib or write C programs that call MATLAB functions using mxArray to access or create
MATLAB variables in CMATLAB provides multiple interfaces to C and C++ language programs.
Note
C++ interfaces are recommended over C-only versions. For more information, see C++ with MATLAB.
For using C/C++ libraries, it is recommended to use the workflow described in Call C/C++ from MATLAB. (since R2022a)
If you need to maintain existing code for calling C shared library functions using the
loadlibraryandcalllibfunctions, see Call C from MATLAB.To create MATLAB functions that can access external C/C++ functions and libraries from MATLAB, it is recommended to consult Write C++ Functions Callable from MATLAB (MEX Files).
If you need to maintain existing code that calls functions in the C Matrix API, see Write C Functions Callable from MATLAB (MEX Files).
To launch MATLAB from C/C++ programs, evaluate MATLAB functions with arguments, and exchange data between MATLAB and C/C++ programs, it is recommended to consult Call MATLAB from C++.
If you need to maintain existing code that calls functions in the MATLAB engine API for C, see Call MATLAB from C.
If you must write a custom program to interact with MATLAB data — for example, you want to read data from an external application, but you do not have access to the source code — then see Write C Programs to Read MAT File Data.
The recommended way to write C/C++ code to work with MATLAB data is to use the MATLAB Data API for C++. The MATLAB C Matrix API has limited C++ language support.
Categories
- Call C from MATLAB
Directly call C library functions from MATLAB R2021b or earlier, usingcalllibfunction
- Write C Functions Callable from MATLAB (MEX Files)
Create C functions that can be called from MATLAB R2017b or earlier, usingmxArrayto access MATLAB variables and create arrays to return to MATLAB
- Call MATLAB from C
Run MATLAB code from C programs, usingmxArray
- Write C Programs to Read MAT File Data
Read and write MATLAB data from C programs, usingmxArray
- C Matrix API
Use the C Matrix API to create C programs that use themxArraytype to represent MATLAB data