= Using CTL4j components from within Matlab =

This works pretty straightforward, because Matlab comes with support for calling Java code in scripts. However, generics are not supported and therefore you cannot use Location.parseFile() as it returns a LinkedList<String>. In addition to that, it might be necessary to modify Matlab's CLASSPATH so that it can find the CTL4j classes. Apart from that, the code almost looks like a standard CTL4j client application:

{{{
javaaddpath('/home/neocool/software/classpath/jsch-20041105-1.4.jar')
javaaddpath('/home/neocool/projects/wire/ctl4j/build')

loc = CTL.Types.Location('neocool@localhost:/home/neocool/projects/wire/ctl4j/src/Example.Server tcp')
proc = CTL.Process(loc)
javaSys.CTestCI.use(proc)
test = javaSys.CTestCI()
test.add(3, 4)
proc.stopService()
}}}

= Using CTL/C++ components from within Matlab =

In contrast, calling CTL/C++ components is much more difficult. There is the code-generator ''ctlmex.py'' which parses Component Interfaces (CIs) and spits out C code which can be compiled with Matlab's own ''mex'' compiler. To simplify the generated code, there is are helper functions for converting from C types to Matlab types and vice versa (see ''mexhelper.cpp'' if you are interested in the dirty details). The C code can be compiled to a so called ''mexglx'' file, which can be loaded by Matlab. Each ''mexglx'' contains only one function, the mexFunction() which can be called from within Matlab. There is one prebuild mex-function, ''use()'', which specifies the location of specific CTL components, just like its counterpart in C++. While all of this sounds like a rather complicated process, you should be able to convert functional CIs by just running ''ctlmex.py'' and then using the resulting ''mexglx'' files like this:

{{{
use('AddRi', 'add/add.exe -l pipe')
res = add2(3, 4)
}}}

'''Note:''' There is currently no support for classes or libraries in ''ctlmex.py''.

= Using Matlab code via the CTL protocol =

Another code-generator, called ''dotm2cpp.py'', is able to parse (simple) ''.m'' files and generate:

1. A thin C++ wrapper around the code generated by Matlab's ''mcc'' compiler
2. A functional CI specifying the function from the Matlab script
3. The necessary CTL_connect() code, which is needed to compile a working component

After compiling this and linking it to a shared library compiled by ''mcc'', you will get a fully-functional CTL/C++ component which calls a function you wrote in Matlab via Matlab's C API.

 * Example ''.m'' file:
{{{
function y = fun2(x1, x2)
    y = x1*x2;
}}}
 
 * Example CTL/C++ client which calls that function via the generated CTL component:
{{{
#!c
#include <mlab_fun2.ci>

int main (int argc, char **argv)
{
    const char *loc = "mlab_fun2.exe";
    ctl::link lnk(loc);
    try
    {
        Matlab::use(lnk);
        int a = 5, b = 6;
        int foo = Matlab::fun2<int,int,int>(a, b);
        std::cout << foo << "\n";
    }
    catch (ctl::exception &e)
    {
        std::cerr << e << "\n";
    }
    return 0;
}
}}}


All of the example code is available by downloading the attached tarball or any CTL4j release >=0.9.6. The included code-generators provide a starting point for coupling larger and more complex Matlab projects and CTL components. Refer to the included shell-scripts to find out which environment settings are needed for running Matlab dependant code. Be aware of the fact that the code was tested against Matlab version 7 only and that this version requires you to use GCC version 3.3 and '''not''' a later or earlier version, because of possible C++ ABI incompatibilities.