\section{Introduction to the MATLAB C/C++ API}

\subsection{MATLAB's C/C++ API}

When interfacing with MATLAB, one first needs to know how to convert between the
types your programming language provides and the types MATLAB uses. The class
\textit{mwArray} provides a mapping between MATLAB's intrinsic types and C++.
For C programmers, there is the type \textit{mxArray}, which basically provides
the same functionality in C-style (no object-orientation).

All the data passed to or gotten from Matlab needs to be converted to the type
\textit{mwArray}, which can hold a single value of the fundamental types
integer (signed or unsigned and 8, 16 or 32 byte long), single (single
precision floating point number) or double. It can also hold a string or an
multi-dimensional array. For example, you can use a constructor which takes
the number of rows and columns, plus the type of fields (e.g. mxDOUBLE\_CLASS)
and the domain (real or complex), to get a matrix-type which interfaces with
MATLAB. The \textit{SetData()} function can set all the fields in a mwArray
from an array of the corresponding type in C++.

Of course, this approach decreases usability and portability, therefore
additional wrapper code was developed and integrated into some code-generators
which will be presented later, which handles all the conversion from C++
to MATLAB types without user-intervention.

\subsection{Introduction to the MCC}

The MCC is a wrapper around GCC, which generates C++ wrapper code around an
existing MATLAB .m function. This gets compiled and linked to MATLAB's own
shared libraries to produce native executables or shared libraries which
can be used without starting the whole MATLAB application.

% TODO: More on the matrix/vector bug.

The MCC version x.x contains a known bug in terms of matrices []. Entries of
rows and columns are interchanged by maintaining the size of the matrix.
Initially this bug seems to be clear, but it causes problematic results and
handling can be tricky. When using the provided code-generator, the rearranging
of matrices is done automatically.

\subsection{Introduction to MEX}

MEX stands for MATLAB executable. MEX-files are dynamically linked subroutines,
which can be used from within MATLAB just like built-in functions or scripts.
The external interface functions provide mechanisms for converting data
between native and MATLAB types, as well as making built-in functions 
available to the external code. Normally, they are used to improve performance
by using native code, but in our case, they are used to make CTL components
usable from MATLAB for integration and also to enable MATLAB to make use of
resources which are available remotely. Chapter \ref{mexglx} will introduce
all the concepts of writing such a MEX-file manually.

\subsection{MATLAB vs. MCR}

There are two possibilities to run a CTL component which interfaces a MATLAB
code. One way is to use the common MATLAB installation. Typically in this
case the MATLAB licence server is contacted and thus one licence is taken.
In general as many licences are blocked as such components are involved. Thus
this approach is not the recommended one.

An alternative way is by using the MATLAB Component Runtime (MCR) []. The MCR is
a virtual machine which can execute wrapper code generated by the MCC. To get 
the machine dependant MCR, a MATLAB script has to be invoked. It results
in compressed package, which contains the required MCR data. This package can be
extracted in a arbitrary path of the desired machine. As the MCR does not 
contact the MATLAB's licence server, this is the cheaper possibility. 

More information on using MCR can be found in the MATLAB documentation 
(\cite{MCR}).

% vim: ft=tex
