Interfacing ArtiSynth to MATLAB

10 Managing an external MATLAB connection from code

The MATLAB connection is associated, internally, with a structure called MatlabInterface, which provides the internal methods for sending data to and from MATLAB. The interface can be obtained and queried from methods in the Main class:

MatlabInterface openMatlabConnection()

Returns a connection to a MATLAB process. If a connection already exists, that connection is returned. Otherwise, a new connection is established with a MATLAB process running on the user’s machine. If no such process is found, then a new MATLAB process will be started. If ArtiSynth is being run under MATLAB, then the connection will be made to the parent MATLAB process

MatlabInterface getMatlabConnection()

Returns the current MATLAB connection, if any; otherwise, returns null.

boolean hasMatlabConnection()

Returns true if a MATLAB connection currently exists.

boolean closeMatlabConnection()

Closes any current MATLAB connection, returning true if a connection previously existed.

Methods by which a MatlabConnection can send data to and from MATLAB include:

void objectToMatlab (obj, matlabName)

Sets the MATLAB array named matlabName to the values associated with the ArtiSynth object obj. The ArtiSynth object must be either a Matrix, Vector, or double[][]. The assigned MATLAB array is dense, unless the ArtiSynth object is an instance of SparseMatrix, in which the array is sparse.

double[][] arrayFromMatlab (matlabName)

Takes the MATLAB array named by matlabName and returns the corresponding double[][] object, with values assigned in row-major order. If the named MATLAB array does not exist, or is not dense and 2-dimensional, then null is returned.

Matrix matrixFromMatlab (matlabName)

Takes the MATLAB array named by matlabName and returns a corresponding Matrix object. If the named MATLAB array does not exist, or is not 2-dimensional, then null is returned. Otherwise, either a MatrixNd or SparseMatrixNd is returned, depending on whether the array is dense or sparse.

Matrix matrixFromMatlab (matlabName)

Takes the MATLAB array named by matlabName and returns a corresponding VectorNd object. If the named MATLAB array does not exist, or is not 2-dimensional with a size of 1 in at least one dimension, then null is returned.

The above methods are also available in the Jython interface ( (section “Jython Interaction and Scripting” of the User Interface Guide) via the functions

   objectToMatlab (obj, matlabName)
   arrayFromMatlab (matlabName)
   matrixFromMatlab (matlabName)
   vectorFromMatlab (matlabName)

The following example shows a code fragment in which a MATLAB connection is obtained and then used to transfer data to and from MATLAB:

   import artisynth.core.driver.Main;
   import artisynth.core.util.MatlabInterface;
   ...
   Main main = Main.getMain();
   MatlabInterface mi = main.openMatlabConnection();
   // read a matrix MX from MATLAB. Assume we know a-priori that
   // MX is dense, so it will be returned as a MatrixNd
   MatrixNd MX = (MatrixNd)mi.matrixFromMatlab ("MX");
   // send a sparse matrix K to MATLAB and assign it the name KMAT
   SparseMatrix K = ...
   mi.objectToMatlab (K, "KMAT");