Connecting Java and Matlab

The matlab desktop runs a java virtual machine. To use a java class, you must first import it. For example, you could type
import javax.swing.*;
You can then use any of these classes, but you must make some changes in syntax. The following is a list of those that come to mind: For example, try this:
import javax.swing.*
J = JFrame('Hi there')
L = JLabel('A Label');
P = J.getContentPane
P.add(L)
J.setSize(200,200);
J.setVisible(1)
You can also import your own classes. There are two things that you must get working in order to do this. Let's now give VectorChooser a spin:
>> import VectorChooser
>> v = VectorChooser(4)
 
v =
 
VectorChooser[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,resizable,title=Vector Chooser,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=null,alignmentY=null,border=,flags=2,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
 
>> v.setVisible(1)
>> v.setSize(250,200)
>> v.getAll

ans =

     0
     0
     0
     0

>> v.setAll(10*rand(1,4))
>> v.getAll

ans =

     9
     2
     6
     4

>> v.setOne(1,0)
>> g.getOne(0)

and = 
 
     9

>> v.getOne(0)
Try moving the sliders. You'll see that matlab can read them!

You can also have a java process running within matlab call matlab, but that is a story for another day.


Recompiling Classes

Unfortunately, if you recompile your java class, matlab will not know about it until you quit and restart matlab. However, Paul Milenkovic has found a cure for this. The rough idea is that you create a classloader for your class, and then access it through the classloader. After you recompile, you kill the classloader and then create a new instance of it, which then reads the recompiled class. Please see Paul Milenkovic's page for more information. Maybe I'll create an example using VectorChooser if there is confusion.
Dan Spielman
Last modified: Tue Feb 10 16:46:51 EST 2004