Undocumented Secrets of MATLAB
9.1 Approaches for Java Control of MATLAB
As explained in previous chapters, MATLAB can easily call Java. Unfortunately, the reverse,
calling and controlling MATLAB from Java, is not so simple. MathWorks has taken a con-
scious decision not to support or document this functionality.
Note that this entire chapter is based on many hours of experimentation and online research
and so while we (Joshua and Yair) are more or less certain of the following, it could be decient
or incorrect in many respects.
Generally speaking, seven technologies can be used to call MATLAB from within Java:
RMI/JMI (RMI for connection, JMI for the functionality) ◾
JNI (connecting to the supported C/C++ engine library using Java Native Interface) ◾
COM (Windows-only; connecting to ◾ MATLAB.Application ProgID or its like)
DDE (Windows-only; using Dynamic Data Exchange) ◾
Dedicated interface via — a Java/MEX communications wrapper ◾
Using MathWorks’ commercial Java Builder toolbox ◾
Most of this chapter will focus on the RMI/JMI approach. JMI (Java–MATLAB Interface)
is widely used by MATLAB to interface the Java and the MATLAB codebases. RMI (Remote
Method Invocation) can be used to run JMI on a different computer. In this manner, different
computers can theoretically use a single MATLAB installation.
Programming JMI requires a medium-to-high level of Java experience. It is not a trivial task,
and many complications need to be addressed, including threading, security, method comple-
tion blocking, virtual machine restrictions, and others. In practice, it is difcult to get all the
pieces programmed correctly. This prevents using JMI directly from being practical and reli-
able. Several JMI wrappers were developed which attempt to handle these issues. The latest
JMI wrapper package is called matlabcontrol.
JMI is described in Section 9.2, while matlab-
control is described in Sections 9.3 and 9.4.
The JNI (Java Native Interface) approach uses the fact that MATLAB has a supported library
for C/C++ integration with MATLAB.
The idea, described in Section 9.5, is to load this library
in the Java code and invoke its entry-point methods using JNI.
A separate JNI-based approach uses the fact that R13 (MATLAB 6.5)’s compiler enabled
creation of a shared library from the source MATLAB code. This shared library could then be
imported and used directly in Java code.
Unfortunately, this approach is not possible with new
MATLAB releases and/or compilers, which use MCR.
MATLAB also has fully documented support for a COM (Windows Component
Object Model) interface and process pipes (Unix/Mac) that allow remote communication from
MCR (MATLAB Compiler Runtime) is a binary component that is called by MATLAB-compiled applications in order
to execute core MATLAB functionality.
K13163_Book.indb 544 11/8/2011 8:10:23 PM