Project RRS feed

  • Question


    Can any One tell me how to link MATLAB and JAVA.
    Friday, October 5, 2007 4:10 AM

All replies


    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
    You can also import your own classes. There are two things that you must get working in order to do this.
    • You must tell matlab where to look for classes. To do this, you must make sure that the file "classpath.txt" contains the directories in which you class files reside. There is a default classpath.txt which does contain ".", so changing to the correct directory should work. For information on how to locate "classpath.txt" and create your own (you probably put it in your home directory or a directory called ~/matlab), look at the advice from MathWorks.
      Mathworks provides more help here
      In my current installation, then only way I can get matlab to read java classes is by starting matlab in the directory in which they reside

    • Matlab does not necessarily accept code from the latest compilers. It might be necessary to add the flag "-target 1.3.1" to my call to javac. For example, you can compile my class VectorChooser.java by downloading it and typing:
      javac -target 1.3.1 VectorChooser.java
    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 =
    >> v.setAll(10*rand(1,4))
    >> v.getAll
    ans =
    >> v.setOne(1,0)
    >> g.getOne(0)
    and = 
    >> v.getOne(0)
    Friday, October 5, 2007 9:25 AM