Here is the code, you are looking for:
import matlabcontrol.*;
public class matlabconnect
{
public static void main(String[] args)
throws MatlabConnectionException, MatlabInvocationException
{
// create proxy
MatlabProxyFactoryOptions options =
new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
// call builtin function
proxy.eval("disp('hello world')");
// call user-defined function (must be on the path)
proxy.feval("matlab_file_name");
// close connection
proxy.disconnect();
}
I have tested the program. It is working well. Do not forget to put your matlab file to its default path.