Now that we've seen the use of the MEX API for creating C-functions callable from MATLAB, we'll briefly highlight a few other APIs. It can be a bit difficult to navigate the purpose of all the different APIs now available for MATLAB, even within a single language, but this page will serve as a high-level overview that will allow you to choose the appropriate APIs for your MATLAB project.

Briefly, there are three ways of interfacing with MATLAB: MEX (using external code from MATLAB), Engine (calling MATLAB code in an MATLAB process from an external program), and the MCR (compiling MATLAB code with external code that can run independent of a MATLAB process or installation). Each of these modes has one or more languages that can be interfaced with MATLAB.

Starting with C and C++ APIs, we'll mention the Engine APIs, which allow starting a MATLAB engine and executing functions within it, as well as the C Matrix API and C++ Data API, which can be used from MEX functions or from programs making use of the Engine API. Though note that the Data API is specific to C++ and requires that you use the C++ MEX API or the C++ Engine API; the C++ and C APIs cannot be mixed.

Important note for using the Engine API

If you attempt to use the Engine API, it is essential to have /bin/csh installed on your system (a symbolic link to tcsh will also work). Without this you will get a silent error, which does not indicate the source of the problem.

While the Data API (C++-only) is the most advanced, the C Matrix API can be used with either C MEX functions or with applications using the Engine API. And as was already pointed out, most languages can interface with C, making the C API a useful API to know if you want to work with MATLAB from some other language. But why use the Engine API in the first place? In our experience there are a few use cases. You may be writing a more sophisticated application that makes use of a general-purpose programming language and its associated libraries, but still need some specific functionality provided by MATLAB or functionality written for MATLAB. Although perhaps not the most common use case in this category, writing a web-server that talks to MATLAB through the Engine API could be one such example.

Though we've primarily considered the C and C++ Engine APIs, there are a number of Engine APIs available:

One other way to use some of the data-oriented APIs, such as the Matrix API and Data API, is through the MCR (MATLAB Compiler Runtime); the MCR lets you call into MATLAB functionality without having to install MATLAB. This could have a few benefits, as there is no overhead or startup cost to interfacing with a MATLAB ENGINE, nor is there a need to have a MATLAB license to run the compiled program (though you will still need a MATLAB license to do the compiling). Using the MCR is a bit like using the Engine in that we are not writing code to be used from MATLB, but rather calling MATLAB code, but it isn't always as easy to use; if you are using MATLAB code that requires external libraries itself, then all of this MATLAB code would need to be compiled to C code via the MATLAB compiler while still interfacing with external libraries - this could be quite an undertaking.

Finally, there are .NET and and Java APIs as well, though we do not discuss them further in this topic.

Octave, which we noted generally has very good compatibility with MATLAB, has a different internal C++ API (as Octave is written in C++), which users can call on. While MEX functions are supported for legacy code, it is not recommended to use them in Octave due to MEX being less performant than the primary Octave APIs. Other APIs discussed here are not supported in Octave, though analogous C++ APIs exist.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement