Saturday 1 December 2007

Matlab call java "class"

1. Every methods should be public, especially the constructor. Otherwise the class can not be called.
Programming only within java, not so strict. Even you don't need put any identifier in front classes and functions.

2. Matlab and Sun Java are not compatible completely, though 99% do, especially when Sun Java or Matlab update, issues always come out. But I found Matlab call the java classes generated by gcc-java(gcj) works much better.
When Matlab updates, it almost work on or for latest java jdk. Matlab 2007a can not call the java class generated by jdk 1.6.03, while Matlab 2007b can.

3. Remember the javaclasspath variable in Matlab. To use javaaddpath(pwd) for you Matlab functions. If new class file is added, run clear;clc; again to initialize the new class.

3.2. About compiling
Put the class file in the static javaclasspath (which is set in /toolbox/local/classpath.txt), in this way it will much faster than javaaddpath(pwd). But you much also put the class file in the classpath of MCR, /toolbox/local/classpath.txt, in this file, there still saying "$matlabroot" things, but here $matlabroot is mean . Just put the class in the mcr classpath folder, no need to be same with .

4.Can not get the attributes from the returned object directly, you need to compose a getXXXX(){return XXXXXX} method by yourself.

5. mcc -m XX.m -a YY.txt
If the XX.m doesn't call java class (including Matlab embeded java functions, here try to use import less, which always cause error when the m-file is compiled), the YY.txt added by mcc without format changed will be able to be accessed, it is stored in XX_mcr/XX/. If not, the YY.txt can not be accessed. You must copy the YY.txt manually to the XX.exe folder.
Just be careful the added files when you mcc Matlab programs using java.

The attached code is to read config file abc.txt.
##################################
// GetConf.java
// GetConf.java
import java.util.*;
import java.io.*;

public class GetConf {
//the public is not necessary for Matlab, but for jython, this is essential.

String para;
Properties conf;

public GetConf() throws IOException{
conf = new Properties();
para = new String();
File file_name = new File("abc.txt");

FileInputStream conf_input;

try {
conf_input = new FileInputStream(file_name);
conf.load(conf_input);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

public String getOnePara(String pnm){
para = conf.getProperty(pnm);
para = para.trim();
return para;
}

public static void main(String[] args) throws IOException{
String pa = (new GetConf()).getOnePara("a");
System.out.println(pa);
}

}
##################################
Here is the config file abc.txt
-------------------------------------------------
# a comment
! a comment

a = a string
b = a string with escape sequences \t \\ \" \' \ (space) \u0123
c = a string with a continuation line \
continuation line
d.e = another string
------------------------------------------------

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter