Miscellaneous‎ > ‎

Matlab and external java (Processing)

Matlab is a native of Java.  It has traveled oceans and continents to get to where it is today and it promises you the dream that is interoperability with java classes.

Like with most things java, you must first setup the "class path" so that the proper name-spaces are observed.  The dynamic path (meaning that you will have to set this up every time and it gets searched last) is managed with the two commands: javaaddpath and javarmpath

Some libraries must be included in Matlab's static path, which is located here: <MATLABPATH>/toolbox/local/classpath.txt

As an example I will show how to get a Processing 1.1 sketch to work with Matlab 2009b in OSX.

To begin, exit Matlab if it is open.  Then open Matlab's static class path:


Add the path of Processing's core.jar to the end of classpath.txt. The path to the Processing files is most likely something like:


Now restart Matlab and you can add dynamic paths to the directories containing your java code.  For this example I add two directories:

javaaddpath({'/Users/Slug/Source/matlab_and_java', '/Users/Slug/Source/matlab_and_java/jtb/test'})

Then it is possible to access the java classes in a straightforward way, pretty much like you would if you were just using java.  Here is an example m-file:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import processing.core.*;

% create window in which applet will execute
applicationWindow = JFrame( 'An applet running as an application' );
width = 300;
height = 200;

% create one applet instance
appletObject = jtb.test.matlab_test(width,height-22);

% call applet's init and start methods

% attach applet to center of window
applicationWindow.getContentPane.add( appletObject );

% set the window's size
applicationWindow.setSize( width, height );

% showing the window causes all GUI components
% attached to the window to be painted

Jeff Bingham,
Apr 27, 2010, 7:25 AM
Jeff Bingham,
Apr 27, 2010, 7:41 AM