To test your installation and environment, build the arrayProduct.cpp
MEX file that is included with MATLAB®. The source code for this MEX example is in subfolders of matlabroot
/extern/examples/cpp_mex
. Be sure that you have a MATLAB-supported compiler installed.
To build a code example, first copy the file to a writable folder on your path. You can use the arrayProduct.cpp
example to test your build environment. This MATLAB command copies the arrayProduct.cpp
file to your current folder.
copyfile(fullfile(matlabroot,'extern','examples','cpp_mex','arrayProduct.cpp'),'.')
To set up and build MEX files, use the mex
command. First set up the environment for C++.
mex -setup C++
Select the installed compiler that you want to use when prompted by the mex
setup script. Then call the mex
command to build your program. Ensure that C++ MEX supports the compiler you select. For an up-to-date list of supported compilers, see Supported and Compatible Compilers.
mex arrayProduct.cpp
The mex
command saves the callable file in the current folder.
Call the arrayProduct
MEX function using the file name without the extension. Pass a scalar multiplier and an array of doubles as arguments. The MEX function multiplies each element in the input array by the multiplier and returns the result.
a = arrayProduct(2,[1 2;3 4]) a = 2 4 6 8