Build C MEX Function
Note
It is recommended to use clibPublishInterfaceWorkflow to write code in MATLAB® R2023a or later to directly call functions in C/C++ libraries.
Alternatively, to write MEX functions using modern C++ features and the MATLAB Data API for C++, see Write C++ Functions Callable from MATLAB (MEX Files).
This example shows how to build the example C MEX function arrayProduct. Use this example to verify the C build
configuration for your system. For information about building C++ MEX functions, see
Build C++ MEX Programs.
Verify that you have installed a Supported and Compatible Compilers. For information about installing a compiler, visit the vendor website.
mex -setup C
Copy the source MEX file to a writable folder on your path.
copyfile(fullfile(matlabroot,'extern','examples','mex','arrayProduct.c'),'.','f')
Call the mex command to build the
function.
mex arrayProduct.c -R2018a
This command creates the file
arrayProduct., where
extext is the value returned by the mexext function.
Test the function. The arrayProduct function takes a scalar and
multiplies it by a 1xN matrix. Call arrayProduct like a MATLAB function.
s = 5; A = [1.5, 2, 9]; B = arrayProduct(s,A)
B =
7.5000 10.0000 45.0000