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:
- In matlab, you do not need to type "new"--objects are created as needed.
- In matlab, you use 'single quotes' where you use "double quotes" in java.
- In java you call a routine with no inputs by putting () after its name. This is unnecessary in matlab.
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)