How can I interface MATLAB code with C, C++, or FORTRAN?

127 views (last 30 days)
I would like to develop certain parts of my code in C, C++, or FORTRAN and others in MATLAB.
How do I develop an application that uses a combination of MATLAB with the three programming languages?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Feb 2016
Section 1: Calling C, C++, or FORTRAN code from MATLAB.
One can call your existing C, C++, or FORTRAN programs from MATLAB by creating MEX-files.
What is a MEX file?
MEX is a built-in utility that enables you to call C, C++, or FORTRAN code in MATLAB by compiling your code into a MATLAB Executable called a MEX-file. MEX-files are dynamically linked subroutines that are called as regular MATLAB functions. This requires you to replace your application's main() with a special gateway function - called "mexFunction" - to pass inputs and outputs to and from MATLAB. MEX is discussed in the MATLAB External Interfaces Guide. For in-depth examples and other detailed MEX information, see the MEX-files Guide, Technical Note 1605. To use MEX, you do not need additional MathWorks products; all you need is MATLAB and a supported C, C++, or FORTRAN compiler. On 32-bit Windows, a supported C compiler (LCC) is distributed with MATLAB. See the Supported and Compatible Compiler list for a complete list of supported C compilers. For example, to compile foo.c into a MEX function, use the following syntax:
mex foo.c
The documentation has several examples of C and C++ MEX-files and FORTRAN MEX-files.
Section 2: Calling MATLAB from C, C++, or FORTRAN stand-alone programs.
The MATLAB Engine allows C, C++, or FORTRAN programs to communicate with a separate MATLAB process via pipes in UNIX and through ActiveX on Windows. For more information on the MATLAB Engine, see using the MATLAB Engine.
Section 3: Converting my MATLAB code into a C or C++ shared library.
You can convert MATLAB code into a C or C++ shared library using the MATLAB Compiler. This shared library can then be incorporated into a Visual Studio project, or be combined with a driver file into a stand-alone application. To compile your MATLAB code, for example foo1.m and foo2.m, into a C shared library use the following syntax:
mcc -B csharedlib:libmyfoo foo1.m foo2.m
For an example on building a C shared library and a driver file, refer to the C shared library example of the documentation. It is further possible to integrate these shared libraries into Visual Studio applications.
Section 4: Knowing when one would need to buy MATLAB Compiler and when one would need only MATLAB.
If one has C, C++, or FORTRAN code and wants to
(a) generate MEX-files to execute C, C++, or FORTRAN code in MATLAB, or
(b) link to a MATLAB session in order to execute MATLAB code in C, C++, or FORTRAN in a stand-alone program
you need MATLAB and a supported C, C++, or FORTRAN compiler to compile the source code.
If you have C or C++ code as well as MATLAB code and you want to
(a) create a C or C++ shared library to integrate into an existing application and custom MATLAB code, or
(b) create a COM/ .NET object from MATLAB code using MATLAB Builder for .NET to integrate with an existing Visual Studio application, or
(c) create an Excel plug-in from MATLAB code using MATLAB Builder for Excel to integrate with an existing Excel project
you need MATLAB, MATLAB Compiler, and a supported C, C++, or FORTRAN compiler to compile the source code.
The following link will provide a list of supported C, C++, or FORTRAN compiler to compile the source code.
 
Section 5: *MATLAB Coder *can be used to generate C code from MATLAB code. The generated source code is portable and readable. MATLAB Coder supports a subset of core MATLAB language features, including program control constructs, functions, and matrix operations. It can generate MEX functions that let you accelerate computationally intensive portions of MATLAB code and verify the behavior of the generated code.
More information on MATLAB Coder can be found at:
 

More Answers (0)

Products


Release

R2011b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!