1

I need help to connect between Java and MATLAB via MatlabControl .

firstly i wanted to test code in walkthrough but every time i got same exception :

myfunc.m :

function myfunc()
    disp('hello from MYFUNC')
end

Java:

class ShowImage {

 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.eval("addpath('E:\\vm')");
        proxy.feval("myfunc");
        proxy.eval("rmpath('E:\\vm)");

        // close connection
        proxy.disconnect();

}}

and here's exceptions :

Exception in thread "main" matlabcontrol.MatlabConnectionException: Could not launch MATLAB. Command: [matlab, -r, javaaddpath 'C:\Users\walaa\Desktop\matlabcontrol-4.0.0.jar'; matlabcontrol.MatlabClassLoaderHelper.configureClassLoading(); javarmpath 'C:\Users\walaa\Desktop\matlabcontrol-4.0.0.jar'; matlabcontrol.MatlabConnector.connectFromMatlab('PROXY_RECEIVER_42dd44d9-815e-41ad-bec9-e469718b5710', 2100);]
    at matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:305)
    at matlabcontrol.RemoteMatlabProxyFactory.requestProxy(RemoteMatlabProxyFactory.java:116)
    at matlabcontrol.RemoteMatlabProxyFactory.getProxy(RemoteMatlabProxyFactory.java:134)
    at matlabcontrol.MatlabProxyFactory.getProxy(MatlabProxyFactory.java:81)
    at ShowImage.main(ShowImage.java:19)
Caused by: java.io.IOException: Cannot run program "matlab": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:292)
    ... 4 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more

I don't know what's the problem, any help will be appreciated!!

1 Answer 1

1

I assume you are using the example from here. Obviously it is tested and working, just make sure that MATLAB is on the Windows path (the error message suggests that it cannot find the program "matlab.exe")

screenshot

Sign up to request clarification or add additional context in comments.

4 Comments

thank u very much :) now it's opening matlab from java , but do u know if i can pass image path to matlab from java via MatlabControl and vice versa or not ?? cause i'm making java GUI to match between two figures one i open on java GUI and another one output from matlb matching functions ,,,please if u can guide me a little bit in MatlabControl functions cause i'm new to it ,,,thanks again .
@user1318251: try passing the image name when you call your MATLAB function in the line: proxy.eval("myMatlabFunction('/path/to/image.png')");
i did what u told me but i got this error "Error using eval Undefined function 'hgr' for input arguments of type 'char' " and my matlab function is
can u help me here i asked new question about that stackoverflow.com/questions/11279998/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.