0: Download
Go here: Download
1: Configure Matlab
Run Matlab and print:
>> edit classpath.txt
press Enter.
Here you see: classpath.txt. Go to the last line and put the path to the downoaded library, then add (!) empty string:
Save and restart Matlab.
2: How to call
Create new java-project and include downloaded library:
3: Example
To execute matlab function “sum” from java use the next line:
Object[] outputArgs =
matlabClient.executeMatlabFunction("sum",inArgs, 1);
code:
import com.jamal.*; import com.jamal.client.MatlabClient; public class Run0 { public static void main(String[] args) { try { MatlabClient matlabClient = new MatlabClient( MatlabCaller.HOST_ADDRESS, "C:\\Program Files\\MATLAB\\R2010a\\bin\\matlab.exe", 40); // First we pass an array of integers and calculate sum in Matlab Object[] inArgs = new Object[1]; inArgs[0] = new int[] { 1, 2, 3, 4 }; Object[] outputArgs = matlabClient.executeMatlabFunction("sum", inArgs, 1); double[] result = (double[]) outputArgs[0]; System.out.println("The sum 1+2+3+4=" + result[0]); matlabClient.shutDownServer(); } catch (JamalException e) { e.printStackTrace(); } } }
log:
The matlab will run RMI server and start processing calls from your client. To stop server use the matlabClient.shutDownServer(); line.
Also you can stop the server manually by running jamal.jar and pressing “stop” button:
Good luck in your work.
See project website for more details: http://jamal.khadkevich.org
Hi, i have a problem, i have done this but cant do it.
Jamal::Matlab host 127.0.0.1 seems to be dead. Trying to launch MatlabServer on the localhost…
Jamal::Trying to connect to MatlabServer… 0
please help me how can solve this problem ?
Hi,
Can you describe your problem in more detail?
Hi, i have a problem, i have done this but cant do it.
Jamal::Matlab host 127.0.0.1 seems to be dead. Trying to launch MatlabServer on the localhost…
com.jamal.JamalException: Jamal::Matlab host 127.0.0.1 seems to be dead
at com.jamal.client.MatlabClient.(MatlabClient.java:85)
at Run.main(Run.java:14)
Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at com.jamal.client.MatlabClient.(MatlabClient.java:64)
… 1 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.(Socket.java:425)
at java.net.Socket.(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
The stack trace tells that the server part of JAMAL is not running. Try to start Matlab and run the following command in Matlab prompt before calling functions from java:
»com.jamal.server.MatlabServer
If you see the following line you are lucky:
Jamal::MatlabServer is ready
interesting article.. thanks for sharing.. 🙂
Hi,
how I can stop the server directly from the console matlab?
thanks
thanks for your help , it is very useful
please i want to know if i have my own function in m file on matlab how can i execute it from java
hi Sara,
you need to add the library jamal
I send you a example
———————————————————————-
in Java:
MatlabClient matlabClient = new MatlabClient(MatlabCaller.HOST_ADDRESS,”C:/Program Files/MATLAB/R2011a/bin/matlab.exe”,40);
/* meto los argumentos de la funcion
como ejemplo en este caso sin argumentos
*/
Object [] ArrayInputs = new Object[0];
/*llamada a la funcion que va a realizar la segmentacion */
Object [] SalidaFuncion= matlabClient.executeMatlabFunction(“test”,ArrayInputs ,1);
matlabClient.shutDownServer();
———————————————————————
I hope you serve as a guide
Thanks your instructions helped me learn this. I am eager to know…if I can even run MATLAB simulations and MATLAB 3d animations through Java.