MatlabProxy¶
-
public abstract class
MatlabProxy
implements MatlabInteractor¶ Communicates with a running MATLAB session. This class cannot be instantiated, it may be created with a
MatlabProxyFactory
. Interaction with MATLAB occurs as if callingeval
andfeval
in the MATLAB Command Window.Communicating with MATLAB Methods which interact with MATLAB provide Java objects to the MATLAB environment and retrieve data from the MATLAB environment as Java objects. The following description of how the conversion between MATLAB and Java types occurs is based on tests conducted on versions of MATLAB R2007b through R2010b. Unless otherwise noted the behavior was identical on all tested versions. However, this behavior is not officially documented by The MathWorks and may change for future versions of MATLAB. (matlabcontrol is not compatible with versions prior to MATLAB R2007b.) MATLAB to Java Numeric Classes and Logical Class All MATLAB numeric types whether they are a singular value or an array/matrix (of any dimension) are always converted into a one-dimensional Java
double[]
. For complex numbers, only the real component is converted. MATLABlogical
s whether a singular value or an array/matrix (of any dimension) are always converted into a one-dimensional Javaboolean[]
. MATLAB arrays are stored in a linear manner, which has been documented by The MathWorks. It is in this linear manner that MATLAB arrays are returned to Java. (Sparse matrices are stored differently and are not sent to Java in an easy to use manner.) Character Class MATLABchar
singular values are returned as a JavaString
. One-dimensional MATLABchar
arrays are returned as a JavaString
. Two-dimensional MATLABchar
arrays are returned as a JavaString[]
with each row of the MATLAB array becoming a JavaString
. MATLABchar
arrays of more than two dimensions have an inconsistent conversion to a Java type, although all observed conversions are either a JavaString
or an array ofString
s. Cell and Struct Arrays MATLABcell
arrays andstruct
arrays are converted to a JavaObject[]
, often with arrays inside of them. Function Handles and Non-Built-In Classes MATLABfunction_handle
s and all non-built-in classes (such as theMap
class or user defined classes) are converted to an instance ofcom.mathworks.jmi.types.MLArrayRef
that is notSerializable
which prevents it from being transferred to a Java application running outside MATLAB (more information on this can be found in the exception section below). Java to MATLAB Primitives It is not possible to directly send a Java primitive to the MATLAB environment because all methods which interact with MATLAB take in eitherObject
orObject[]
which results in the Java primitives becoming their auto-boxed class equivalent. (For exampleint
becomesInteger
.) Java primitive arrays, such asint[]
areObject
s and can therefore be sent to the MATLAB environment. They are converted as follows:Java Type MATLAB Type boolean[]
logical
arraychar[]
not supported* byte[]
int8
arrayshort[]
int16
arrayint[]
int32
arraylong[]
not supported* float[]
single
arraydouble[]
double
arrayIn MATLAB R2009b and higher, MATLAB will throw an exception. In MATLAB R2008b and earlier, MATLAB will segfault. The behavior in MATLAB R2009a is not known. *Numbers Subclasses of
Number
, which includes all of the auto-boxed versions of Java primitive numeric types, become MATLABdouble
s. BooleansBoolean
s are converted to MATLABlogical
s. CharactersCharacter
s are converted to MATLABchar
s. StringsString
s are converted to MATLABchar
arrays. Object Arrays Arrays of non-primitive types are converted to MATLABcell
arrays. The contents of the array are converted according to these same rules. Note that Java’s multidimensional arrays are not an exception to this rule. For instance adouble[][]
is an array ofdouble[]
s and so MATLAB will create a cell array of MATLABdouble
arrays. Other Classes Classes not otherwise mentioned remain as their original Java type. Objects contained within that class or instances of that class are not automatically converted by MATLAB, although when fields or methods are accessed in the MATLAB environment they may be converted into MATLAB types as documented by The MathWorks. Behavior of transferred data How Java objects sent to MATLAB or retrieved from MATLAB behave depends on several factors: Running outside MATLAB References to Java objects are copies. (There is one exception to this rule. Objects that arejava.rmi.Remote
will act as if they are not copies. This is because matlabcontrol communicates with MATLAB’s Java Virtual Machine using Remote Method Invocation.) Running inside MATLAB References to Java objects in MATLAB that are returned to Java, reference the same object. When passing a reference to a Java object to MATLAB, if the Java object is not converted to a MATLAB type then it will reference the same object in the MATLAB environment. Help transferring data Thematlabcontrol.extensions.MatlabProxyLogger
exists to record what is being returned from MATLAB. Thematlabcontrol.extensions.MatlabTypeConverter
can convert between complicated Java and MATLAB types. Currently only MATLAB numeric arrays are supported. Exceptions Proxy methods that are relayed to MATLAB can throwMatlabInvocationException
s. They will be thrown if:- An internal MATLAB exception occurs. This occurs primarily for two different reasons. The first is anything that would normally cause an error in MATLAB such as trying to use a function improperly or referencing a variable that does not exist. The second is due to the undocumented nature of the underlying Java MATLAB Interface API, such as trying to send a
long[]
to MATLAB. - The proxy has been disconnected via
disconnect()
. - Communication between this Java Virtual Machine and the one that MATLAB is running in is disrupted (likely due to closing MATLAB).
- The class of an object to be sent or returned is not
java.io.Serializable
orjava.rmi.Remote
. 1 Java primitives and arrays behave as if they wereSerializable
. - The class of an object to be returned from MATLAB is not defined in your application and no
SecurityManager
has been installed.2 - The class of an object to sent to MATLAB is not defined in MATLAB and the class is not on your application’s classpath.3
- The method call is made from the Event Dispatch Thread (EDT) used by AWT and Swing components.4 (A
matlabcontrol.extensions.CallbackMatlabProxy
may be used to interact with MATLAB on the EDT.) This does not apply toexit()
which may be called from the EDT.
1This is a requirement of Remote Method Invocation, which matlabcontrol uses when running outside MATLAB. 2 This is due to Remote Method Invocation prohibiting loading classes defined in remote Java Virtual Machines unless a
SecurityManager
has been set.PermissiveSecurityManager
exists to provide an easy way to set a security manager without further restricting permissions. Please consultPermissiveSecurityManager
‘s documentation for more information. 3 MATLAB sessions started by aMatlabProxyFactory
are able to load all classes defined in your application’s class path as specified by thejava.class.path
property. Some frameworks load classes without placing them on the class path, in that case matlabcontrol will not know about them and cannot tell MATLAB how to load them. 4 This is done to prevent MATLAB from hanging indefinitely. When interacting with MATLAB the calling thread (unless it is the main MATLAB thread) is paused until MATLAB completes the requested operation. When a thread is paused, no work can be done on the thread. MATLAB makes extensive use of the EDT when creating or manipulating figure windows, uicontrols, plots, and other graphical elements. For instance, callingplot
from the EDT would never return because theplot
function waits for the EDT to dispatch its event, which will never occur, because the thread has been paused. A related, but far less critical issue, is that pausing the EDT would make the user interface of MATLAB and any other Java GUI code running inside MATLAB non-responsive until MATLAB completed evaluating the command. Thread Safety This proxy is unconditionally thread-safe. Methods which interact with MATLAB may be called concurrently; however they will be completed sequentially on MATLAB’s main thread. Calls to MATLAB from a given thread will be executed in the order they were invoked. No guarantees are made about the relative ordering of calls made from different threads. This proxy may not be the only thing interacting with MATLAB’s main thread. One proxy running outside MATLAB and any number of proxies running inside MATLAB may be simultaneously connected. If MATLAB is not hidden from user interaction then a user may also be making use of MATLAB’s main thread. This means that two sequential calls to the proxy from the same thread that interact with MATLAB will execute in that order, but interactions with MATLAB may occur between the two calls. In typical use it is unlikely this behavior will pose a problem. However, for some uses cases it may be necessary to guarantee that several interactions with MATLAB occur without interruption. Uninterrupted access to MATLAB’s main thread may be obtained by use ofinvokeAndWait(...)
. Threads When running outside MATLAB, the proxy makes use of multiple internally managed threads. When the proxy becomes disconnected from MATLAB it notifies its disconnection listeners and then terminates all threads it was using internally. A proxy may disconnect from MATLAB without exiting MATLAB by callingdisconnect()
.Author: Joshua Kaplan See also:
MatlabProxyFactory.getProxy()
,MatlabProxyFactory.requestProxy(matlabcontrol.MatlabProxyFactory.RequestCallback)
- An internal MATLAB exception occurs. This occurs primarily for two different reasons. The first is anything that would normally cause an error in MATLAB such as trying to use a function improperly or referencing a variable that does not exist. The second is due to the undocumented nature of the underlying Java MATLAB Interface API, such as trying to send a
Constructors¶
MatlabProxy¶
-
MatlabProxy
(Identifier id, boolean existingSession)¶ This constructor is package private to prevent subclasses from outside of this package.
Methods¶
addDisconnectionListener¶
-
public void
addDisconnectionListener
(DisconnectionListener listener)¶ Adds a disconnection that will be notified when this proxy becomes disconnected from MATLAB.
Parameters: - listener –
disconnect¶
-
public abstract boolean
disconnect
()¶ Disconnects the proxy from MATLAB. MATLAB will not exit. After disconnecting, any method sent to MATLAB will throw an exception. A proxy cannot be reconnected. Returns
true
if the proxy is now disconnected,false
otherwise.Returns: if disconnected See also:
.exit()
,.isConnected()
exit¶
-
public abstract void
exit
()¶ Exits MATLAB. Attempting to exit MATLAB with either a
eval
orfeval
command will cause MATLAB to hang indefinitely.Throws: See also:
.disconnect()
,.isConnected()
getIdentifier¶
-
public Identifier
getIdentifier
()¶ Returns the unique identifier for this proxy.
Returns: identifier
invokeAndWait¶
-
public abstract <T> T
invokeAndWait
(MatlabThreadCallable<T> callable)¶ Runs the
callable
on MATLAB’s main thread and waits for it to return its result. This method allows for uninterrupted access to MATLAB’s main thread between two or more interactions with MATLAB. If running outside MATLAB thecallable
must bejava.io.Serializable
; it may not bejava.rmi.Remote
.Parameters: - <T> –
- callable –
Throws: Returns: result of the callable
isConnected¶
-
public abstract boolean
isConnected
()¶ Whether this proxy is connected to MATLAB. The most likely reasons for this method to return
false
if the proxy has been disconnected viadisconnect()
or is if MATLAB has been closed (when running outside MATLAB).Returns: if connected See also:
.disconnect()
,.exit()
isExistingSession¶
-
public boolean
isExistingSession
()¶ Whether this proxy is connected to a session of MATLAB that was running previous to the request to create this proxy.
Returns: if existing session
isRunningInsideMatlab¶
-
public abstract boolean
isRunningInsideMatlab
()¶ Whether this proxy is running inside of MATLAB.
notifyDisconnectionListeners¶
-
void
notifyDisconnectionListeners
()¶ Notifies the disconnection listeners this proxy has become disconnected.
removeDisconnectionListener¶
-
public void
removeDisconnectionListener
(DisconnectionListener listener)¶ Removes a disconnection listener. It will no longer be notified.
Parameters: - listener –