MEX Matlab EXecutable

MATLAB is a popular programming language for scientific computing and data analysis. It provides a wide range of tools and functions for numerical computation, visualization, and data analysis. MATLAB's flexibility and ease of use have made it a popular choice for research, engineering, and education. One of the key features of MATLAB is its ability to create executables using the MATLAB Compiler.

MATLAB Executables, or MEX files, are executable files created from MATLAB code. They are stand-alone, platform-specific files that can be run on computers that do not have MATLAB installed. MEX files are typically used when you need to deploy MATLAB code to others who do not have MATLAB or need to distribute your code without giving away the source.

In this article, we will explain what MEX files are, how to create them, and how to use them.

What is a MEX file?

A MEX file is a platform-specific executable file that is created from MATLAB code using the MATLAB Compiler. MEX files are typically used to speed up MATLAB code, or to distribute MATLAB code to others who do not have MATLAB installed on their computer. MEX files are similar to DLL files in Windows or shared libraries in Linux.

A MEX file contains compiled MATLAB code that can be called from other programming languages like C or C++. The MATLAB Compiler provides a set of tools that can be used to create MEX files. These tools include the MATLAB Compiler, the MATLAB Builder NE, and the MATLAB Builder JA.

Why use a MEX file?

There are several reasons why you might want to use a MEX file:

  1. Speed: MEX files can significantly speed up MATLAB code. MATLAB is an interpreted language, which means that the code is executed line by line. This can be slow for large datasets or complex algorithms. By creating a MEX file, you can compile the code into a binary format that can be executed much faster.
  2. Portability: MEX files can be used on computers that do not have MATLAB installed. This makes them useful for distributing MATLAB code to others who do not have MATLAB, or for deploying MATLAB code on a server.
  3. Intellectual Property Protection: By creating a MEX file, you can protect your MATLAB code from being copied or modified. The MEX file contains compiled code, so it is much harder to reverse engineer than the original MATLAB code.
  4. Integration with other languages: MEX files can be called from other programming languages like C or C++. This makes it easy to integrate MATLAB code with existing code in other languages.

How to create a MEX file?

Creating a MEX file involves several steps:

  1. Write MATLAB code: The first step is to write the MATLAB code that you want to compile into a MEX file. This code can be written in the MATLAB environment or in an external text editor.
  2. Compile MATLAB code: The next step is to compile the MATLAB code using the MATLAB Compiler. The MATLAB Compiler is a separate product that must be installed separately from MATLAB. Once installed, the MATLAB Compiler can be used to create MEX files.
  3. Create a MEX file: Once the MATLAB code has been compiled, the MEX file can be created. The MEX file can be created using the MATLAB Builder NE or the MATLAB Builder JA. These tools provide a graphical user interface that makes it easy to create MEX files.
  4. Test the MEX file: Once the MEX file has been created, it is important to test it to ensure that it works correctly. This can be done by running the MEX file in MATLAB or by calling it from another programming language like C or C++.

Example: Creating a MEX file

Let's look at an example of how to create a MEX file. In this example, we will create a MEX file that calculates the dot product of two vectors using MATLAB code.

  1. Write MATLAB code: The first step is to write the MATLAB code that calculates the dot product of two vectors. Here is an example of the code:matlabCopy codefunction c = dot_product(a, b) % calculate the dot product of two vectors c = dot(a, b); end
  2. Compile MATLAB code: The next step is to compile the MATLAB code using the MATLAB Compiler. To do this, we need to open the MATLAB Compiler and create a new project.
  3. Create a MEX file: Once the project has been created, we can add the MATLAB code to the project and compile it into a MEX file. To do this, we need to select the MATLAB code and click on the "Build" button.
  4. Test the MEX file: Once the MEX file has been created, we can test it to ensure that it works correctly. To do this, we can run the MEX file in MATLAB or call it from another programming language like C or C++.

Here is an example of how to call the MEX file from C++:c++Copy code#include <iostream>#include "mex.h" int main(){ // create two vectors    double a[] = {1, 2, 3}; double b[] = {4, 5, 6}; // call the MEX file    mxArray* plhs[1]; mxArray* prhs[2]; prhs[0] = mxCreateDoubleMatrix(1, 3, mxREAL); prhs[1] = mxCreateDoubleMatrix(1, 3, mxREAL); memcpy(mxGetPr(prhs[0]), a, 3 * sizeof(double)); memcpy(mxGetPr(prhs[1]), b, 3 * sizeof(double)); mexCallMATLAB(1, plhs, 2, prhs, "dot_product"); // get the result    double* c = mxGetPr(plhs[0]); std::cout << "Dot product: " << c[0] << std::endl; // free memory    mxDestroyArray(plhs[0]); mxDestroyArray(prhs[0]); mxDestroyArray(prhs[1]); return 0; }

Conclusion

MEX files are a powerful feature of MATLAB that allow you to create stand-alone, platform-specific executables from MATLAB code. MEX files can significantly speed up MATLAB code, or be used to distribute MATLAB code to others who do not have MATLAB installed. MEX files can also be called from other programming languages like C or C++. By following the steps outlined in this article, you can easily create MEX files for your own MATLAB code.